Compare two records in X++ D365FO

To compare tow records in dynamics 365 for operations you can use this code snippet that was created as a runnable class(job)

class job2Compare2Records
 /// <summary>
 /// Runs the class with the specified arguments.
 /// </summary>
 /// <param name = "_args">The specified arguments.</param>
   public static void main(Args _args)
     Table          table1,table2;
     RefRecid       recId1 = 68719517265;
     RefRecid       recId2 = 68719517264;

     container compareRecords(Common _record1, Common _record2)
          DictTable dictTable = new SysDictTable(_record1.TableId);
          DictField dictField;
          FieldId fieldId, extFieldId;
          container ret;
          int i, j;
          if (_record1.TableId != _record2.TableId)// checking if the same table records
               return conNull();
          int u = dictTable.fieldCnt();
          for (i=1; i<=u; ++i)
              fieldId  = dictTable.fieldCnt2Id(i);
              dictField = new SysDictField(_record1.tableId, fieldId);
              if (!dictField.isSystem())
                  for (j=1; j<= dictField.arraySize(); ++j)
                       extFieldId = fieldId2Ext(fieldId, j);
                       if (_record1.(extFieldId) != _record2.(extFieldId))
                            ret += [strfmt ("%1, %2, %3",fieldId2Name(_record2.TableId, extFieldId), _record1.(extFieldId), _record2.(extFieldId))];
          return ret;
      select table1 where table1.Recid == recid1;
      select table2 where table2.Recid == recid2;
      container a = compareRecords(table1 ,table2);

      for (int i=1; i <= conlen(a); i++)
          info(strFmt("container value %1 - : %2", i, conpeek(a, i)));


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s