Search through blog..

Monday, October 29, 2012

Microsoft Dynamics Ax Maps

In Dynamics Ax, several of the features in the Account Payable module and the Account Receivable module are very similar, therefore even most of the main tables, transactions tables and setup tables are similar. With similar features the business logic is also similar, therefore making it possible to reuse much of the code. However, although the tables and their fields may appear similar in their construction, the names used are likely to be quite different.  This is where Table Maps are used. 
 
Maps define X++ elements that wrap table objects at run time. With a map, you associate a map field with a field in one or more tables. Maps are often referred to as table maps to differentiate from the foundation class map. A map is declared just as a table. In fact only the name shows a map is used. So it is recommend suffixing the name of the map with *Map making it easier to read the code.
 
In maps, typically only fields are created and mapped, as properties are already specified at the mapped table. You can use a map as any table from X++ or objects (like forms or reports).
 

Example (1): Creating a map, mapping common fields in MyFirstTable & CustTable.
  1. Locate the node Maps, right-click and select New Map. Rename the map to “MyFirstMap” using the property sheet.
  2. Drag the extended data types AccountNum, CustName, CustGroupId and CurrencyCode to the Fields node.
  3. Save MyFirstMap, right-click on the same and select Restore.
  4. Go to the node Mappings, right-click and choose New Mapping. Open the property sheet for the new mapping and select the table CustTable. A line for each of the fields in MyFirstMap is now added to the new mapping.
  5. Use the property sheet for each of the mapping fields to specify the related field in the property MapFieldTo. The related fields from CustTable are accountNum, name, currency and custGroup.
  6. Repeat step 5 by adding a map for the table MyFirstTable.
  7. Go to the node Methods and create the following method:
    void listRecords(MyMap _myMap)
    {
    ;
    while select _myMap
    {
    info(strFmt("%1, %2", _myMap.accountNum, _myMap.custName));
    }
    }
  8. Save the map.

As mentioned above, you will have to restore the map otherwise changes will not be shown in mappings before restarting the client. And the method added is to test the map. This is a simple method printing a value from the current record the map is initialized with.

Also create a separate job with following content and execute it:
static void DataDic_TestMyMap(Args _args)
{
MyFirstMap myFirstMap;
CustTable custTable;
MyFirstTable myFirstTable;
;
myFirstMap.listRecords(myFirstTable);
}


Output:


As shown, the map can be initialized with any of the tables declared in the map and the records in that particular table will be shown in the Infolog.

The methods of a map are similar to those of a table, enable to you to create or modify methods that act on the map fields. You can initialize fields, check modified fields and validate before saving or deleting. Calling a default method like insert() on a map will execute the corresponding insert() on the mapped table.

Tip: While working with Dynamics AX maps, you might have come across the below error - Error executing code: The field with ID '0' does not exist in table 'CustTable'.
This error will occur most likely for missing a mapping in a table map.

Let us modify our own example to understand better, In the above job, replace parameter ‘myFirstTable’ with custTable.
myFirstMap.listRecords(custTable); 
And to repro the error I have created the mappings as shown below:






Now when you execute the job with CustTable as parameter, you will get an error because the mapping for CustTable is incomplete.




So now we know the reason for this kind of error, and the solution to resolve it is to check and update the mappings properly.
 
 
Post a Comment