Reflections : Changing properties of AOT objects in code

Few days back i was working to update some properties of dynamics ax objects, objects count was quite high and manually doing that work could be hectic. i decided to use the reflection and i found it real powerful as it has lots of area where needs playing.

My scenario was simple where i need to find objects on my own criteria and then update the properties for those objects.

like i have to find out all those classes which starts with ‘XXX’

static void FindClassStartsWithXXX(Args _args)
 {
   UtilIdElements utilId;
 ;
   while select utilId where utilid.recordType == UtilElementType::Class
   && utilId.name like 'XXX*'
   {
     print utilId.name;
   }
   pause;
 }

i also have to update properties of selected objects, like here i am updating the name property of the class.

static void RenameXXXObjectsToYYY(Args _args)
 {
   TreeNode  node = TreeNode::findNode(@'\Classes\XXXClass');
   str oldName;
   str newName;
   #Properties
   ;
   oldName = node.AOTgetProperty(#PropertyName);
   newName = strdel(oldName,1,3);
   node.AOTsetProperty(#PropertyName, "YYY"+newName);
   node.AOTsave();
   node.treeNodeRelease();
   node = null;
 }

There are a couple of things to explain here. First of all the macro #Properties contains the names of all properties you can read and write. These are the same as what you get in the property window. Use the macro instead of literal strings, it’s safer and doesn’t violate best practices.

Next you can see there is a set of methods to get and set properties. After modifying anything you need to save the changes, just like you would when changing an AOT object in the property window. TreeNode works just like doing it manually. You can do a lot more fun stuff with the TreeNode API.

Also, if you have questions or ideas for articles don’t hesitate to let me know .

%d bloggers like this: