Dynamics AX makes us the RefRecId, RefTable and RefCompany data pattern in a number of tables the system to provide flexible linking from a common/generic table such as WorkflowWorkitemTable to a variety of specific tables such as PurchReq or PurchTable.
Examples of this include Workflowtables, DocuRef, SpecTrans, etc…
From time to time you may want to identify which of the records in these tables have become “orphaned” i.e. the documents that they refer to no longer exist. To do this in a fairly generic way you can make use SysDictTable’s makeRecord functionality as below (Using DocuRef as an example)
static void Check_Orphans(Args _args)
while select forupdate * from docuref// where SpecTrans.RefTableId == 865 && SpecTrans.RefCompany==’an’// 866 for Debtors
table = new SysDictTable(docuref.RefTableId);
record = table.makeRecord();
select firstonly * from record where record.RecId == docuref.RefRecId;
info(strFmt(“Record %1 has been orphaned”, docuref.caption()));
//Delete if necessary over here…
info(strFmt(“%1 orphaned records found”,i));
I hope you find this a useful trick in your AX wanderings.