Friday, July 27, 2018

To check the mandatory fields in a table through X++ code



static void checkProperties(Args _args)
{
    DictTable       dictTable;
    DictField       dictField;
    int             i, cnt;
 
    dictTable = new DictTable(tableNum(CustTrans));
    cnt = dictTable.fieldCnt();
    for (i= 1; i<=cnt;i++)
    {
        dictField = new DictField(tableNum(CustTrans),dictTable.fieldCnt2Id(i));
        if (dictField.mandatory())
        {
            info (strFmt("Field %1 is mandatory.",dictField.label()));
        }
    }
   
}


How to Open Project through X++ Code in Ax 2012


static void OpenProjectThroughCode(Args _args)
{
    ProjectNode     projectType,projectNode;

    projectType = infolog.projectRootNode().AOTfindChild('Shared');
    projectNode = projectType.AOTfindChild('Your Project Name Which You want to open');
    if(projectNode)
    {
        projectNode.getRunNode();
    }

}

How to Get Selected Customer all Transaction using Morphx Reports in Ax 2012





  • Design a Form as Below 

  • Here I Added a Button Named as Print 
             I have Overrided Clicked Method
              void clicked()
              {
                    Args args = new args();
                    ReportRun reportRun;
                    ;
                    args.parm(Customers.valueStr());       //Customers indicates Lookup name
                    args.name(reportstr("Custtrans"));     //Custtrans indicates Morphx report name
                    reportRun = classFactory.reportRunClass(args);
                    reportRun.init();
                    reportrun.run();
                   //reportRun.wait();
                   //args.parmObject( args );
                   super();
                }
  • Added a StringEdit control Named as Customers               //Auto declaration Yes
            I have overrided Lookup Method
          public void lookup()
         {
                Query query = new Query();
                QueryBuildDataSource queryBuildDataSource;
                QueryBuildRange queryBuildRange;
                SysTableLookup sysTableLookup =               SysTableLookup::newParameters(tableNum(custTable), this);
                sysTableLookup.addLookupField(fieldNum(CustTable, AccountNum));
                sysTableLookup.performFormLookup();
               //super();
           }
  • Design a Morphx Report
     Under Report override Init Method
     public void init()

    {
    Query query1 = new Query();
    QueryBuildDataSource queryBuildDataSource;
    QueryBuildRange queryBuildRange;
    str var;
    ;
    var = element.args().parm();
    try
    {
      if(element.args().parm())
      {
           query1.addDataSource(tablenum(CustTrans)).addRange(fieldnum(CustTrans, AccountNum)).value(queryValue(element.args().parm()));
           this.query(query1);
           this.query().userUpdate(false);
           this.query().interactive(false);
           super();
      }
     }
     catch(exception::Error)
     {
       info("Error in init method");
     }
}
  • Now Open Form and Select Customer and Click on Print 
  • Finally Report appears for Selected Customer (Here I selected US-013)

Create Table Using X++ Code



static void autoTable(Args _args)
{
SysDictTable sysdictTable;
Treenode treenode;
AOTTableFieldList fieldnode;
str prop;
int pos;
#AOT
#Properties
;
//Coded by Gautam Verma

//#Table path refer the \\Data Dictionary\\Tables and finding the path
treenode = treenode::findNode(#TablesPath);
 
//AOTadd method is to add table in tables//AutoTableis table name
 
treenode.AOTadd('AutoTable');
treenode = treenode.AOTfindChild('AutoTable');
treenode.AOTcompile(1);
treenode.AOTsave();
treenode.AOTfindChild('AutoTable');
fieldnode = treenode.AOTfirstChild();
fieldnode.addString('AccountNum');
fieldnode = fieldnode.AOTfindChild('AccountNum');
prop = fieldnode.AOTgetProperties();
pos = findPropertyPos(prop,#PropertyExtendeddatatype); //find right place to put extended data type
pos = strFind(prop,'ARRAY',pos,strLen(prop));
pos = strFind(prop,'#',pos,strLen(prop));

fieldnode.AOTsetProperties(prop);
treenode.AOTcompile(1);
treenode.AOTsave();
treenode.AOTRestore(); //to load assigned extended data type properties
sysdictTable = sysdictTable::newTreeNode(treenode);
appl.dbSynchronize(sysdictTable.id());
}

Create New Table On New Record Creation Of Another Table



This post will help you if you have requirement to create a new record every time when a new record is created in another table.

Let me explain this with an example:
Assume that you have a master table 'Bank_Account' which contains a field 'AccountNumber'. Now the requirement is  when a new Account Number is added (created) related to this Account Number a new table should be created as 'Transaction_####'. The name of the Transaction_#### would be dynamic.
If the Account Number is 1001 then Transaction_#### name should be Transaction_1001.

Follow the below steps:

  • Create a table Bank_Account
    • Add a field Account_Number (String)
    • Set properties Mandatory : Yes , Allow Edit : No
    • Make Account_Number as a Primary Key 
      • PK : Go to Index node > New Index

    • Field AccountNumber has now become primary key.

  • Override modified field method in table Bank_Account and write following code.
 public void modifiedField(FieldId _fieldId)
{
    SysDictTable sysdictTable;
    Treenode treenode;// its a class
    AOTTableFieldList fieldnode;
    str Prefix,Acc,Tablename,prop;
    int pos,Account_NumberID;
    #AOT
    #Properties
    ;

    Account_NumberID = fieldNum(Bank_Account, Account_Number); // Getting Account_Number field ID
    super(Account_NumberID);
    this.insert();
    Prefix = "Transaction_";
    Acc = this.Account_Number;
    TableName= Prefix + Acc;

//#Table path refer the \\Data Dictionary\\Tables and finding the path
treenode = treenode::findNode(#TablesPath);
//AOTadd method is to add table in tables//TableName is table name
treenode.AOTadd(Tablename);
treenode = treenode.AOTfindChild(TableName);
treenode.AOTcompile(1);
treenode.AOTsave();
treenode.AOTfindChild(TableName);
fieldnode = treenode.AOTfirstChild();
fieldnode.addString('AccountNum');
fieldnode = fieldnode.AOTfindChild('AccountNum');
prop = fieldnode.AOTgetProperties();
pos = findPropertyPos(prop,#PropertyExtendeddatatype); //find right place to put extended data type
pos = strFind(prop,'ARRAY',pos,strLen(prop));
pos = strFind(prop,'#',pos,strLen(prop));
fieldnode.AOTsetProperties(prop);
treenode.AOTcompile(1);
treenode.AOTsave();
treenode.AOTRestore(); //to load assigned extended data type properties
sysdictTable = sysdictTable::newTreeNode(treenode);
appl.dbSynchronize(sysdictTable.id());

}

  • Override insert method of table Bank_Account and write following code

public void insert()
{
    super();
    info("NewTable " + "Transaction_" + this.Account_Number + " has been created");
}
  • An Account is added into Bank_Account








  • Now go to AOT > Table node , a new table named 'Transaction_101' has been created.




How to Change Date format in Ax 2012


static void CON_DateFormat(Args _args)
{
    date todaydate = today();
    str s;

    s = date2str(todaydate,123,DateDay::Digits2,DateSeparator::Hyphen,
DateMonth::Short,DateSeparator::Hyphen,DateYear::Digits4);

    info(strFmt("Today date is " + s));
}

Container and Its Functions in Ax


Container

   X++ (Object oriented programming language) the data or values 
   stored in a Variable are of below types:
  • Primitive dataTypes - int, str, real .... etc.
  • Composite dataTypes - Arrays, Containers, Collection Classes
Container Functions

1.ConPeek( )
  • The conPeek() is used to retrieve a specific element from a container.
  • Syntax: anytype conPeek(container container, int number)
    • container - The container to return an element from.
    • number - The position of the element to return. 
                               Specify 1 to get the first element.
  • Return value: The element in the container at the position specified
 by the number parameter. The conPeek function automatically converts
 the peeked item into the expected return type.
Ex :   static void Ex_Conpeek(Args _args)
         {
               container   c = ['Test', 27,'Rajendra',10.000000000];

               info(strFmt('%1 - %2', conPeek(c, 1), conPeek(c, 2)));
               info(strFmt('%1 - %2', conPeek(c, 1),conPeek(c, 3)));
               info(strFmt('%1 - %2', conPeek(c, 1),conPeek(c, 4)));
         }
























2.ConPoke( )

  • The conPoke() is used to modify a container by replacing 
        one or more of the existing elements.
Syntax: 

  • container conPoke(container container, int start, anytype element, ...)
    • container - The container to modify.
    • start - The position of the first element to replace.
    • element - One or more elements to replace, separated by commas.
  • Return value: The new container with the inserted elements.

Ex : static void Ex_Conpoke(Args _args)
       {
            container   con = ["Rajendra",123]; //intial data 
            con     = conPoke(con,2,143143);  // after modification Done
            info(strFmt("%1 - %2",conPeek(con,1),conPeek(con,2)));
        }

























3.ConIns()

  • conIns() is used to insert one or more elements into a container.
  • Syntax: container conIns(container container, int start, anytype element, ...)
    • container - The container into which to insert elements.
    • start - The position at which to insert elements.
    • element - One or more elements to insert, separated by commas.
  • Return value: The new container with the inserted elements.
Ex : static void Ex_ConIns(Args _args)
{
    container   con = ["Rajendra",410];//intial container Data
    con = conIns(con,3,123456);       //After Data Inserted into Container
    info(strFmt("%1 - %2 - %3",conPeek(con,1),conPeek(con,2),conPeek(con,3)));
}

















4.ConFind( )

  • The conFind() is used to find the first occurrence of an element or a sequence of elements in a container.
  • Syntax: int conFind (container container, anytype element,... )
    • container - The container to search.
    • element - One or more elements to search for, separated by commas.
  • Return value: Returns 0 if the item was not found; otherwise, the sequence number of the item.
Ex:   static void Ex_ConFind(Args _args)
  {
        container   con = ["Rajendra",410];
    // conFind() function finds position in the container that a certain value
    info(strFmt("410 is found at position %1   - Rajendra is found at position %2",conFind(con,410),conFind(con,"Rajendra")));
}


















5.ConLen( )

  • The conLen() is used to retrieve the number of elements in a container.
  • Syntax: int conLen(container container)
    • container - The container in which to count the number of elements.
  • Return value: The number of elements in the container.
Ex : static void Ex_ConLen(Args _args)
{
    container   con = ["Rajendra",123,'A',4.2525];
    info(strFmt("Length of a Container is %1",conLen(con)));
}

















6.ConDel( )
  • The conDel() is used to remove the specified number of elements from a container.
  • Syntax: container conDel(container container, int start, int number)
    • container - The container from which to remove elements.
    • start - The one-based position at which to start removing elements.
    • number - The number of elements to delete.
  • Return value: A new container without the removed elements.
Ex: static void Ex_ConDel(Args _args)
{
    container   con = ["Rajendra",1254];
    con =  conDel(con,2,1);
    info(strFmt("%1--%2 ",conPeek(con,1),conPeek(con,2))); // 0 indicates that 1254 has been deleted & no element is there

}

















7.Connull( )
  • The conNull() is used to retrieve an empty container. Use this function to explicitly dispose of the contents of a container.
  • Syntax: container conNull()
  • Return value: An empty container.
Ex : static void Ex_ConNull(Args _args)
{
    container   con = ["Hello","Welcome","To","Ax",2012,"R3"];
    info(strFmt("Container conNull()  before : %1 - %2 - %3 - %4
 - %5 -%6",conPeek(con,1),conPeek(con,2),conPeek(con,3),conPeek(con,4)
,conpeek(con,5),conPeek(con,6))); 

    con = conNull(); //clears the container

    info(strFmt("Container conNull()  after  : %1 - %2 - %3 - %4 - %5 - %6",conPeek(con,1),conPeek(con,2),conPeek(con,3),conPeek(con,4),
conpeek(con,5),conPeek(con,6)));
    
}