Home
> Ax Technical > Adding a Table field through X++ code in Dynamics Ax
Adding a Table field through X++ code in Dynamics Ax
Sometime back there was a query in one of the discussion forum about inserting a table field through code. I did a small job to demonstrate that, just thought would write that down in my blog too…… and here it is.
I have added sufficient comments to make it explanatory.
static void JobCreateTableFields(Args _args)
{
#AOT
#TreeNodeSysNodeType
{
#AOT
#TreeNodeSysNodeType
//find the Table
TreeNode tablenode = TreeNode::findNode(#TablesPath).AOTfindChild(‘LedgerTable’);
TreeNode fieldNode, tn;
Struct properties;
Struct propertyInfo;
Array propertyArray;
str propertyValue;
AOTTableFieldList lst;
str name;
Counter propertyCount;
int i;
Map map = new Map(Types::String, Types::String);
TreeNode tablenode = TreeNode::findNode(#TablesPath).AOTfindChild(‘LedgerTable’);
TreeNode fieldNode, tn;
Struct properties;
Struct propertyInfo;
Array propertyArray;
str propertyValue;
AOTTableFieldList lst;
str name;
Counter propertyCount;
int i;
Map map = new Map(Types::String, Types::String);
;
//Find the tables field node
lst = tablenode.AOTfindChild(‘fields’);
//add the field
lst.addString(‘Test’);
//now find the node in the tree
fieldNode = lst.AOTfindChild(‘Test’);
//get the properties structure
properties = fieldNode.AOTgetPropertiesExt();
//Update the properties map with the required properties
map.insert(‘ExtendedDataType’, ‘LedgerAccount’);
lst = tablenode.AOTfindChild(‘fields’);
//add the field
lst.addString(‘Test’);
//now find the node in the tree
fieldNode = lst.AOTfindChild(‘Test’);
//get the properties structure
properties = fieldNode.AOTgetPropertiesExt();
//Update the properties map with the required properties
map.insert(‘ExtendedDataType’, ‘LedgerAccount’);
// get the number of properties
// and the array containing the properties structure
propertyCount = properties.value(‘Entries’);
propertyArray = properties.value(‘PropertyInfo’);
// and the array containing the properties structure
propertyCount = properties.value(‘Entries’);
propertyArray = properties.value(‘PropertyInfo’);
for (i = 1; i < propertyCount; i ++)
{
propertyInfo = propertyArray.value(i);
name = propertyInfo.value(‘Name’);
//see if we have inserted the property name in to the map
if (map.exists(name))
{
//set the property
propertyInfo.value(‘Value’, map.lookup(name));
}
}
{
propertyInfo = propertyArray.value(i);
name = propertyInfo.value(‘Name’);
//see if we have inserted the property name in to the map
if (map.exists(name))
{
//set the property
propertyInfo.value(‘Value’, map.lookup(name));
}
}
//set the properties structure
fieldNode.AOTsetPropertiesExt(properties);
fieldNode.AOTsetPropertiesExt(properties);
//save the treenode
tablenode.AOTsave();
//Let us open the table
//and see if the code works 🙂
tablenode.AOTnewWindow();
}
tablenode.AOTsave();
//Let us open the table
//and see if the code works 🙂
tablenode.AOTnewWindow();
}
……………………..
hey can i get the code of droping a table using x++
Hi Kamal,
Nice blog. It helps me to figure this out. But I still had some issues to get this to work.
There is an easier way which is used more times in Ax to set the values of a field. The properties method can be used to sort this out. This one is using the array settings already.
The properties macro contains all the names of the properties.
str properties;
;
#properties
if (fields.AxExtendedDataType)
{
properties = FieldTreeNode.AOTgetProperties();
properties = setProperty(properties,#PropertyExtendeddatatype, fields.AxExtendedDataType);
FieldTreeNode.AOTsetProperties(properties);
}
kind regards,
Johan van Veldhuizen (To-Increase)
Thanks for the comments Johan…
You are very much right, the setProperty is the right way to do it better. I realized it few weeks after this post and i reposted the finding here.
Hello Kamal!
Nice blog entry…!
One question, have you tried to add a field to the AOT without assigning an Extended Data Type?
In my example AX throws an Exception if the Extended Data Type property is not filled.
But for for (some) enums (e.g. ItemType) there is no Extended Data Type 😉
Regards
Simon