Archive

Archive for December 4, 2010

TreeNode AOT Save – Did you know?

Today I had a small requirement from one of my colleague stating he would like to update a particular table field property using X++ code for upgrade purpose.

So I wrote a sample job when I actually found that, if treeNode is traversed all the way to fields and then property is modified and saved. The table still shows that it should to be saved (You can see a red horizontal line prefixed to the table name)

Note: Although if you restore the table you can see the updated value. This gives an immersion to the user that that table needs to save manually else modified field properties value will be lost.

Reason: To set a field property you need to traverse all the way to field node so the treeNode object actually holds the following buffer

NAME:
	TreeNode
VALUE:
Path: \Data Dictionary\Tables\AddressCityTable_BR\Fields\CountryId:gls

Due to which when treeNode.AOTSave () is performed the treeNode object try to perform the save on the field and not on the table. This was bit strange thing to observe in the first place.

So to save the record I had to copy the treeNode instance to other object at the time of finding the table such that once the property is modified, the AOTSave/AOTCompile is made on the copied treeNode object.

NAME:
	treeNode_TableName
VALUE:
Path: \Data Dictionary\Tables\AddressCityTable_BR Layer: gls

By this way I was able to save the record successfully at run-time, without any manual intervention.

Here is the sample code for the following exercise.

static void setTableProperties()
{
    #AOT
    #Properties
    #define.tableName('AddressCityTable_BR')
    TreeNode          treeNode;
    TreeNode          treeNode_TableName;
    AOTTableFieldList aotFields;
    ;

    // Find the table using TreeNode.
    treeNode = infolog.findNode(#TablesPath).AOTfindChild(#tableName);

    // Copying the tables path because using this 
    // INSTANCE we will save the AOT.
    treeNode_TableName = treeNode;

    // Go to the fields Node.
    aotFields = treeNode.AOTfindChild('Fields');

    // Mention the field Name
    treeNode = aotFields.AOTfindChild('CountryId');
    treeNode.AOTsetProperty(#PropertyMandatory, 
                            #PropertyValueNo);

    treeNode_TableName.AOTcompile(true);
    treeNode_TableName.AOTsave();

    treeNode_TableName.treeNodeRelease();
    treeNode.treeNodeRelease();
    treeNode_TableName = null;
    treeNode = null;

    appl.dbSynchronize(tablenum(#tableName));
}

This entry is shared since in most of the articles I found that they directly perform AOTSave on the instantiated treeNode.

Categories: X++ stuffs
%d bloggers like this: