Powered By Blogger

Tuesday, April 6, 2010

Code snipet 2 remove the identical copy of AOT object

For some unknown reasons, sometimes AOT objects are touched in VAR layer but actually are identical copies. When the developer compared the VAR layer object with the one in lower layer (BUS, SYS etc.), AX showed it was an identical copy and no difference as such.

Below is the code example on how can we remove the identical copy in X++ code:

static void FindAndDeleteIdenticalObjects(Args _args)
{
SysTreeNode comparable1, comparable2;
TreeNode curLevelTreeNode, upperLevelTreeNode;
UtilIdElements utilElements, joinUtilElements;
;
while select UtilElements where UtilElements.utilLevel == UtilEntryLevel::var && ( UtilElements.recordType == UtilElementType::Form Utilelements.recordType == UtilElementType::Report Utilelements.recordType == UtilElementType::Table Utilelements.recordType == UtilElementType::Class Utilelements.recordType == UtilElementType::Enum Utilelements.recordType == UtilElementType::ExtendedType
)

{
//Should use join if for a normal table, but not applicable for UtilElements
//Performance hit if use exists join
select firstonly recid from joinUtilElements
where joinUtilElements.utilLevel != UtilElements.utilLevel && joinUtilElements.name == UtilElements.name && joinUtilElements.recordType == UtilElements.recordType;

if (joinUtilElements.RecId)
{
curLevelTreeNode = SysTreeNode::findNodeInLayer(UtilElements.recordType, UtilElements.name, UtilElements.parentId, UtilElements.utilLevel);

upperLevelTreeNode = SysTreeNode::getLayeredNode(curLevelTreenode, 1);
comparable1 = SysTreeNode::newTreeNode(curLevelTreeNode); comparable2 = SysTreeNode::newTreeNode(upperLevelTreeNode);

if (SysCompare::silentCompare(comparable1, comparable2))
{
info(strFmt("Element name: %1, Element type: %2", UtilElements.name, enum2str(UtilElements.recordType)));
//Remove the node
curLevelTreeNode.AOTdelete();
}
}
}
}

It's a bit cumbersome here while pasting, but i think its gives an overview to understand the fact :)

No comments:

Post a Comment