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
   && like 'XXX*'

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;
   oldName = node.AOTgetProperty(#PropertyName);
   newName = strdel(oldName,1,3);
   node.AOTsetProperty(#PropertyName, "YYY"+newName);
   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 .


One Response to Reflections : Changing properties of AOT objects in code

  1. HI Amir,
    Is there a way to detect when an object has been changed?
    Trying to do some logging based on that.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: