Working With Kernel Based Classes and AOT Based Tables in Dynamics AX
Thinking in the Abstract
If you have seen some of my blog articles before, you may have noticed that I look at automating documentation from time to time. This article will expand on the concept a bit, and use different components of Dynamics AX to accomplish or demonstrate some of the more abstract ways to retrieve information.
In the past, I have used TreeNode (https://msdn.microsoft.com/en-us/library/gg958198.aspx) to work with AOT-based objects. However, if working with just tables and fields, a lot of information can be retrieved by using some kernel based classes. I will demonstrate a couple here.
The first kernel based class is the DictTable class (https://msdn.microsoft.com/en-us/library/dicttable.aspx). DictTable allows you to retrieve information specific to a table. To use the object, the X++ code will resemble the following:
DictTable dictTable; TableId tableId = tableNum(CustTable); dictTable = new dictTable(tableId);
At this point, the dictTable object is instantiated and will be referencing the data for the CustTable table. Information about the table can be retrieved via method calls from the object. The DictTable documentation in the link above provides an example of retrieving and displaying some of the information on the table.
The next kernel based class is the dictField class (https://msdn.microsoft.com/en-us/library/dictfield.aspx). DictField is used to retrieve field information from a table. More on this in a moment, as I want to bring in another kernel based object before we start working with DictField.
The next kernel based object I want to bring in is the SQLDictionary table (https://msdn.microsoft.com/en-us/library/sqldictionary.aspx). This table describes the current state of the database with respect to the table and field metadata. For the purposes of our discussion, we need to work with two fields, tabId and fieldId. The tabId column is used to denote fields that exist in a specific table. FieldId is the field number of the field within the specific table. IF the fieldId value is 0, it is the ‘header’ record for the table, and it does not represent an actual field in the table.
NOTE: Do NOT go in and start randomly changing information in the SQLDictionary table. This will cause many issues within the system.
So, why do I bring this up? Well, using SQLDictionary in combination with DictField, you can work in the abstract with table information.
Here is an example:
// define a specific table Id, use the CustTable table in the demonstration TableId tableId = tableNum(CustTable); FieldId fieldId; SqlDictionary sqlDictionary; DictTable dictTable; DictField dictField; while select sqlDictionary where sqlDictionary.tabId == tableId && sqlDictionary.fieldId != 0 { dictField = new DictField(tableId,sqlDictionary.fieldId); info(strFmt("Name: %1, Label: %4, Label Id: %3, Base Type: %2", dictField.name(),dictField.baseType(),dictField.labelDefined(),dictField.label())); }
The above code, when placed in a job, will list out all the fields in the table, the name, the base type (string, int, real, etc.), IF the field has a label defined on it, the label Id will be displayed, and the actual label text for this field. By looking at the documentation page for DictField, you can see what information you can retrieve from the method calls.
Also, using these objects, you can work abstractly, and actually work with data in the tables. Here is an example:
static void AbstractionDemoJob(Args _args) { #define.AccountNum("ACCOUNTNUM") // define a specific table Id, use the CustTable table in the demonstration TableId tableId = tableNum(CustTable); FieldId fieldId; Common buffer; SqlDictionary sqlDictionary; DictTable dictTable; DictField dictField; dictTable = new dictTable(tableId); select firstOnly sqlDictionary where sqlDictionary.tabId == tableId && sqlDictionary.fieldId != 0 && sqlDictionary.name == #AccountNum; if (sqlDictionary) { fieldId = sqlDictionary.fieldId; buffer = dictTable.makeRecord(); buffer.(fieldId) = "BillTest"; buffer.insert(); } }
Notice the code in the if statement. IF we get a SQLDictionary record for CustTable where there is a field named ACCOUNTNUM, we can use that information to fill in data. The above code gets the field Id from the SQLDictionary record, then it uses the common object and DictTable to create a table buffer based on CustTable. Then it sets the AccountNum field to BillTest, and finally inserts the record into the table.
Also, please note that additional information can be retrieved by using the SysDictField object instead of DictField (https://msdn.microsoft.com/en-us/library/sysdictfield.aspx). This inherits from DictField, and expands on the functionality provided.
In summary, as I have stated before, knowing some of the kernel based classes provides some rather unique tools for many different purposes. I hope I have provided some basis for thinking outside of the box on using Dynamics AX based objects.
Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.
Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.