How to create number sequence from code or wizard in Ax 2012




1.       Create an EDT: Car Id.
AOT >> Extended Data Types >> New >> Properties >> Name >> Car Id.

2.       Write a code on lode module() on NumberSeqModuleProject
{
     datatype.parmDatatypeId(extendedTypeNum(Car Id));
     datatype.parmReferenceHelp(literalStr("@SYS334483"));
     datatype.parmWizardIsManual(NoYes::No);
     datatype.parmWizardIsChangeDownAllowed(NoYes::No);
     datatype.parmWizardIsChangeUpAllowed(NoYes::No);
     datatype.parmWizardHighest(999999);
     datatype.parmSortField(20);
     datatype.addParameterType (NumberSeqParameterType::DataArea, true, false);
     this.create(datatype);
}


3. Write a method on Projparameters Table


client server static NumberSequenceReference numRefcarId()
{
    return NumberSeqReference::findReference      (extendedTypeNum(car Id));
}

4. Write a job and run that 


static void Carid(Args _args)
{
    NumberSeqModuleProject  NumberSeqModuleProject = new NumberSeqModuleProject();
    ;
    NumberSeqModuleProject.load();
}

5. Then run the wizard 


Organization Administration >> CommonForms >> Numbersequences>>Numbersequences>> Generate >> run the wizard.

6. Now we have to check the number sequence  is correctly working  for that write a job: 


static void number(Args _args)
{
    NumberSeq  numberSeq;
    CarId num;
    ;
    numberSeq = NumberSeq::newGetNum(ProjParameters::numRefcarId());
    num = numberSeq.num();
    info(num);
}
Run the above job. We will find the generated Number     sequence.                                                 

7. Now we want that Number Sequence in form level (Car Table): Declare the number sequence On Form Declaration:

public class FormRun extends ObjectRun
{
    NumberSeqFormHandler numberSeqFormHandler;
}

8. Write the NumberSeqFormHandler() in form methods node. 

NumberSeqFormHandler numberSeqFormHandler()
{
   if (!numberSeqFormHandler)
   {
        numberSeqFormHandler = NumberSeqFormHandler::newForm(ProjParameters::numRefcarId().NumberSequenceId,
                                                             element,
                                                             CarTable_DS,
                                                             fieldNum(CarTable, Car Id)
                                                            );
    }
    return numberSeqFormHandler;
}

9. Write the close() on the form methods node. 

void close()
{
    if (numberSeqFormHandler)
    {
        numberSeqFormHandler.formMethodClose();
    }
    super();
}

10. Then final add the below methods on data source methods node 


o    Create()
void create(boolean append = false,
            boolean extern = false)  // If created externally
{
    element.numberSeqFormHandler().formMethodDataSourceCreatePre();
    super(append);
    if (!extern)
    {
        element.numberSeqFormHandler().formMethodDataSourceCreate(true);
    }
}
 


o    Delete() 
public void delete()
{
    element.numberSeqFormHandler().formMethodDataSourceDelete();
    super();
}

  
o    Write() 
public void write()
{
    super();
    element.numberSeqFormHandler().formMethodDataSourceWrite();
}

  
o    Validate Write() 
public boolean validateWrite()
{
    boolean         ret;
    ret = super();
    ret = element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret) && ret;
    if (ret)
    {
        CarTable.validateWrite();
    }
    return ret;
}

  
o    Link Active() 
public void linkActive()
{
    ;
    element.numberSeqFormHandler().formMethodDataSourceLinkActive();
    super();
}  


Now our numberseqence is generated .

C​REATE NUMBER SEQUENCE FROM WIZARD
CREATE NUMBER SEQUENCE FROM WIZARD

Organization administration/Common/Number sequences/Number sequences
EXP-CODE='GNR'
>select name
>select scope as company name


GENERATE IT OVERRIDE THE CREATE METHOD IN FORM DATASOURCE

If Company Scope is not Shared Use below Method

o    Create() 
public void create(boolean _append = false)
{
    str strNumberSequenceName;
    super(_append);
    strNumberSequenceName=numberSeq::newGetNumFromCode('YourNumberSequenceCode').num();
    DataSourceName.FieldName=strNumberSequenceName;
}
 
If Company Scope is Shared then Use below Method
  
o    Create() 
public void create(boolean _append = false)
{
    str strNumberSequenceName;
    super(_append);
    strNumberSequenceName=numberSeq::newGetNumFromCode('YourNumberSequenceCode',NumberSeqScope::find(NumberSequenceScope::findByNaturalKey('',0,0,0).RecId)).num();
    DataSourceName.FieldName=strNumberSequenceName;
}

If you don't want to Generate form Wizard you can directly Override Create Method in form DataSource 

//Override create method in Data source
public void create(boolean _append = false)
{
     int max1;
    A_Student _Student;(table name)
    str 60 Value;
    str 60 _newCode;
    ;
    //breakpoint;
    super(_append);
    select firstOnly Student_ID from _Student order by RecId desc;// query to find last number
    if(!_Student.Student_ID)// When table is blank
    {
        _newCode='CT_'+ strRFix(int2str(1),3, "0");//Assigne value to table field
        DSStudent.Student_ID =_newCode;
       // info(U_City_Master.City_Code);
        //StateMaster.StateCode='ST_'+strRFix(int2str(1), 3, "0");//Assigne value to table field
    }
    else
    {
        max1=str2int(subStr(_Student.Student_ID,4,3))+1;// When table is not blank, add 1
        DSStudent.Student_ID ='ST_'+ strRFix(int2str(max1),3, "0");//Assigne value to table field
       // info(U_City_Master.City_Code);
    }// if Close
}

Related Posts

Previous
Next Post »

Thanks for comments.....