Hi, I found this post Rahul's AX Blog copy-data-from-one-table-to-another is very useful, so I just keep it here.
The scenario is you have table AAA, and then duplicate a new table CopyAAA. Then you would like to copy the data records across them.
The code is written as d365FO job. However it works on ax 2012, 2009 and 4.0 as well.
class CopyAcrossTable
{
public static void main(Args _args)
{
void buf2buf(Common _from, Common _to)
{
DictTable dictTableFrom = new DictTable(_from.TableId);
DictTable dictTableTo = new DictTable(_to.TableId);
DictField dictFieldFrom;
FieldId fieldIdFrom = dictTableFrom.fieldNext(0);
FieldId fieldIdTo
;
while (fieldIdFrom && ! isSysId(fieldIdFrom))
{
dictFieldFrom = new DictField(_from.TableId, fieldIdFrom);
if(dictFieldFrom)
{
fieldIdTo = dictTableTo.fieldName2Id(dictFieldFrom.name());
if(fieldIdTo)
_to.(fieldIdTo) = _from.(fieldIdFrom);
}
fieldIdFrom = dictTableFrom.fieldNext(fieldIdFrom);
}
}
AAA aaa;
CopyAAA copyAAA;
;
while select aaa
{
buf2Buf(aaa, copyAAA);
copyAAA.insert();
}
info("Copy done!");
}
}
Until the next post!