Tuesday, 29 December 2015

Dynamics AX setup forms for customer creation defaults

On creating a new customer in AX, the value for currency, country and language is defaulted automatically. These are the forms from where these default values come from:



Currency:

General ledger à Setup à Ledger à Accounting currency


Country:


File à Tools à Options à Default country/region

Language:

Organization administration à Setup à Legal entities à Language

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. 

Wednesday, 24 December 2014

How to update the InstanceRelationType field.

If you are using table inheritance in AX, you may have created base and derived tables. The InstanceRelationType field in the base table is an indicator that the record in base table belongs to which derived table. This field contains the TableId of the derived table. There can be scenarios in which you configure some sample/ demo data or meta data for your application in these tables and you may want this data to be available with each application installation. You might have generated some sql scripts/ or using Sql DTS(Data Tranformation Service) to copy that data for each installation.
The problem that you will face in that case is that for every fresh installation of your application on AX i.e. deployment of your code on AX, Your tables and all other objects are created with a new unique ID. A new tableId field is generated for each installation.
This will cause your setup data not to work with the new installation since the table ids are now different on the new environment so in the base table, AX can not identify which record belongs to which table.
You might think of manually checking the table id for all the derived tables and try to update them in an AX job by X++ code but AX will not allow you to update the InstanceRelationType  field.
The solution to update this field is to change it from Sql Server. A Sql script can be generated to update this field accordingly in the base table but even for that script, you will require the TableId/ Id fields for each derived table which you can get from AX. Getting the table id from AX is an additional step if you are working on AX R2 or R3. But if you are working on R1, then you can easily get the TableId from Sql as well.. which means you just have to execute a script with a click of a button and it will do all the work. Here is the sample Sql code to get the Table id field in R1.


declare @tableIdMyAxDerivedTable
as int = (select tableid from SQLDICTIONARY where NAME = 'MyAxDerivedTable and FIELDID = 0)

print @tableIdMyAxDerivedTable

After fetching the correct table id, lets update that in our data..

update MyBaseTable
set INSTANCERELATIONTYPE = @tableIdMyAxDerivedTable -- Current table id in AOT
where INSTANCERELATIONTYPE = 98789 -- Old table id in my data


To do the same task in AX R2 or R3... the solution is to create a job in AX, and place the above written sql update code in a string. You can easily fetch the table id from AX and replace that fetched table id in that sql query.
You can print that string by calling the info method...All you have to do now is to run that string/Sql query in Sql server.

One method to convert any data type to String.

In AX, you can use the "strFmt" method. This method can convert any data type to string. There is no need to write separate methods for each data type if you want to convert them to string.


Here is some sample code. I have declared and initialized some default AX data types. These data types are converted to string, which is printed in the info log.

    int i;
    real r;
    date dt;
    CustomerTransactionType cct; //AX base enum
    
    i = 10;
    r = 5.5;
    dt = today();
    cct = CustomerTransactionType::FreeTextInvoice;
    
    
    info(strFmt('%1',i));
    info(strFmt('%1',r));
    info(strFmt('%1',dt));
    info(strFmt('%1',cct));

Tuesday, 23 December 2014

Calling table insert and update methods conditionally.

In AX, there can be situations where you have to write code for table insert and update. Lets say you are exposing some AX logic by AIF or manipulating data with temp. tables.  In these situations, you may develop only one method both for Insert and update. All the table fields data can be passed to this method by parameters. The important question is when to conditionally execute the Insert and Update method. The answer is the RecID field. RecId is always greater than zero for any record that is being selected from the database. So for that condition you can call the update method. Similarly, if the table buffer is populated with all the fields except the RecID field, then it means that  you have just filled all those fields and Insert methods needs to be called for that condition. To implement this logic all you have to do is to write the update and insert method in a If condition like:

If(Table1.Recid  > 0 )
{
Table1.update();
}
Else
{
Table1.insert();
}

Monday, 15 December 2014

How to change AX layer

If you want to change the current AX layer, lets say from USR to ISV then follow the following steps.

1) Open "Microsoft Dynamics AX Configuration Utility".

2) Under the "Developer" tab, there is a field named as "Application object layer to open". From a list      of available layers, change this field's value from USR to ISV.

3) Enter the development license code that you have for the ISV layer.

4) Restart the AX client, it will now be opened in the ISV layer.

All the AX objects that you will create now or import through any XPO, will be created on the ISV layer.