Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
Забыли пароль?
Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

Опции темы Поиск в этой теме Опции просмотра
Старый 04.05.2009, 20:05   #1  
Blog bot is offline
Blog bot
25,232 / 843 (79) +++++++
Регистрация: 28.10.2006
SysDictCoder: Changing properties of AOT objects in code

A while ago I wrote about reflection in Dynamics Ax. In the article I mentioned that it was also possible to modify the AOT and that I’d give some examples. Well, I kind of forgot. Sorry about that. Todd Hensley was kind enough to remind me of that promise and presented an interesting example problem.
I have a list of tables where I want to set the ClusterIndex to be the same as the PrimaryIndex.

I’ve been able to loop through an array of the table names, look them up using DictTable, and examine the PrimaryIndex and ClusterIndex values.

But I can’t figure out how I would say:
myTable.clusterIndex = myTable.primaryIndex;

The properties all appear to be read-only.

Any ideas?

Thank you for the question and, yes, I do have an idea.

Enter the TreeNode

The key to solving this is knowing that there is more than one way to do reflection. As Todd found out, the Dictionary API is read-only. But the TreeNode API is not.

TreeNode is exactly what its name says: a node in a tree.
I can hear you thinking: “Nodes? Tree? What are you talking about?”

I’m talking about the Application Object Tree. The AOT has a bunch of nodes and a TreeNode object can represent any one of those nodes, like Forms, a class method or a table.

Whenever you’re using the AOT, clicking on an object and setting properties you’re using TreeNodes. And the best part is you can do it in code too!

Getting the node

Enough talk. Time to show the code. First you need to get hold of a node. Let’s use CustTable as an example.

TreeNode node = TreeNode::findNode(@'\Data dictionary\Tables\CustTable'); ; info(node.treeNodePath()); info(node.AOTname());

As you can see, you need to enter the path similar to the way you open the nodes AOT. If findNode() fails it will return null.

Once you have the node the fun starts. In this case I’m just dumping something in the infolog to show you it works.


Now let’s do what we really want. We’re going to set the primary index as the cluster index.

TreeNode node = TreeNode::findNode(@'\Data dictionary\Tables\CustTable'); str indexName; #Properties ; indexName = node.AOTgetProperty(#PropertyPrimaryIndex); node.AOTsetProperty(#PropertyClusterIndex, indexName); 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.

Finally you need to release the memory held by the TreeNode. The garbage collector cannot clean up TreeNode objects by itself. It needs a little help from you by calling treeNodeRelease() when you’re done. In this example nothing bad would happen if you don’t. However, if you run this in a loop a few thousand times Ax will run out of memory. Not something you really want.

Fitting it in with DictTable

Todd already did a lot of work with DictTable to find the tables he needs to change. And now I tell him DictTable won’t cut it. Not really helping.

Not all is lost. DictTable has a method to get a TreeNode object. How convenient. If we change the above example to start with a DictTable object we end up with this:

SysDictTable dictTable; TreeNode node; str indexName; #Properties ; dictTable = SysDictTable::newTableId(tableNum(CustTable)); node = dictTable.treeNode(); indexName = node.AOTgetProperty(#PropertyPrimaryIndex); node.AOTsetProperty(#PropertyClusterIndex, indexName); node.AOTsave(); node.treeNodeRelease(); node = null;

This can be adapted to fit in a loop over several tables and Todd’s problem is solved.

You can do a lot more fun stuff with the TreeNode API. I’ll cover that in another installment. I won’t forget this time. I promise :)

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

Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
aot, treenode, x++


Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
To find objects in AOT with particular properties Blog bot DAX Blogs 1 26.04.2008 07:33
axStart: How to find line of code with a question mark in your AOT. Blog bot DAX Blogs 0 16.04.2008 22:05
C# and AX Development: How to delete AOT objects (AX/Axapta) Blog bot DAX Blogs 0 13.12.2007 19:50
Malaysia: How to Run Time Add Code to AOT Blog bot DAX Blogs 0 18.09.2007 15:00
при построении перекрёстных ссылок выдаётся сообщение об ошибках mmmax DAX: Программирование 10 21.01.2005 12:42
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Часовой пояс GMT +3, время: 02:02.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.