Skip to main content

X++ code to remove identical copy

Our client asked for a job to remove the identical copy from VAR layer.
For some unknown reason, some AOT objects are touched in VAR layer but actually are identical copy. When the developer compared the VAR layer object with the one in lower layer (BUS, SYS etc.), AX showed it was an identical copy.
Here is the example on how you can 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)

{
//Thanks for Jim Shepherd here

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();

}

}

}

}

Popular posts from this blog

Dynamics Axapta: Sales Orders & Business Connector

Well, again folllowing my same idea of writting close to nothing and pasting code, I'll paste in some code to create a sales order from some basic data and the invoice it. I'll try to explain more in the future. AxaptaObject axSalesTable = ax.CreateAxaptaObject("AxSalesTable"); AxaptaRecord rcInventDim = ax.CreateAxaptaRecord("InventDim"); AxaptaRecord rcCustTable = ax.CreateAxaptaRecord("CustTable"); rcCustTable.ExecuteStmt("select * from %1 where %1.AccountNum == '" + MySalesOrderObject.CustAccount + "'"); if (MySalesOrderObject.CurrencyCode.Trim().Length == 0) MySalesOrderObject.CurrencyCode = rcCustTable.get_Field("Currency").ToString().Trim(); string sTaxGroup = rcCustTable.get_Field("taxgroup").ToString().Trim(); //set header level fields axSalesTable.Call("parmSalesName", MySalesOrderObject.SalesName.Trim()); axSalesTable.Call("parmCustAccount", M

Passing values between form and class

Class name is EmplDuplication and Form is EmplTable . void clicked() {    MenuFunction mf;    args args = new Args();    ;     args.record(EmplTable);     mf = new menufunction(identifierstr(EmplDuplication), MenuItemType::Action); mf.run(args); } Meanwhile, in the main() method of the EmplDuplication class, we need to put this Axapta x++ code to get the datasource: static void main(Args args) {     EmplDuplication EmplDuplication; EmplTable localEmplTable; ;     if(args.record().TableId == tablenum(EmplTable)) localEmplTable = args.record();     ... }