Friday 16 January 2015

How to get the count of specific records in x++

To get the count of records in x++, see the example given below. Here, in an AX job, we are getting count of all the sales orders that belong to a specific customer account.


static void JobCount(Args _args)
{
    SalesTable salesTable;
       
    select  count(RecId) from salesTable
    where salesTable.CustAccount == '1101';

    info(strFmt('%1',salesTable.RecId));    // Display the count value.
}

Thursday 15 January 2015

How to use a display method in a grid

In a form's grid control, along with the normal data source columns, if you want to show some calculated/computed column as well then you can use a display method for that purpose.  

First, create a display method in a form's data source. The sample code for the display method is:

public display Str60 getTotalRecordsCount(MyTableName _myParamTableName)
{
      MyAnotherTable myAnotherTable ;

     //Select count of specific records in a table
      select count(RecId) from myAnotherTable 
     where myAnotherTable.myTableNameRefRecid == _myParamTableName.RecId;

      // Return that count
     return int642str(myAnotherTable .RecId);
}

To utilize this method, add a new StringEdit field in the grid. Set the properties for this StringEdit control i.e. mention the above mentioned method name in the DataMethod Property. Mention the form's data source name (which will be passed as a parameter to this display method) in the DataSource property.

Mentioning the DataSource property is necessary to show correct data.
Also the return type of the display method is intentionally not set to "str" , here the return type is "Str60". Making the return type as "str" will increase the grid rows height.