Compare Records D365FO & AX 2012





How to compare two records buffer field to field. 



public static container compareRecords(Common _record1, Common _record2)
{
    SysDictTable    dictTable = new SysDictTable(_record1.TableId);
    SysDictField    dictField;
    FieldId         fieldId, extFieldId;
    container       ret;
    int             i, j;
    ;

    if (_record1.TableId != _record2.TableId)
        return conNull();

    for (i=1; i<=dictTable.fieldCnt(); ++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 += [extFieldId, _record1.(extFieldId), _record2.(extFieldId)];
                }
            }
        }
    }

    return ret;
}




For Demo purpose you can use below code in job/runnable class.


static void demoCompareRecords(Args _args)
{
    VendTable vendTable_1 = VendTable::find('ABC'); 
    VendTable vendTable_2 = VendTable::find('XYZ'); 
 
    container   con;
    int         i;
    ;
 
    con = MyClass::compareRecords(vendTable_1 , vendTable_2 );
 
    for (i=1; i<=conLen(con); i+=3)
    {
        info(strFmt("%1, %2 ,%3"
                   ,fieldId2Name(tableNum(VendTable), conPeek(con, i))
                   ,conPeek(con, i+1)
                   ,conPeek(con, i+2)
                   )
             );
    }
}







Support Faryal's Cusine


Pass field value from SalesTable to CustTrans in AX 2012 & D365FO



Pass field value from SalesTable to CustTrans in AX 2012 & D365FO


AX 2012 

Following are the steps to pass the value from Sales Table to custtrans.

1) Create a new field (If not already existing)

2) Create the parm method for that field on CustVoucher class

3) Assign value to this parm method in SalesInvoiceJournalPost.postCustVend() method by using the salesTable buffer

4) Add the code for assigning the variable value(or the parm method value) in CustVoucher.initCustVendTrans() method

And now post an invoice and go to custTrans table to check the field value.

Source AX-2012

D365FO

Add the field in SalesTable 
Add the field in CustTrans
Create Extension class of SalesInvoiceJournalPost
Override postCustVend method using coc 

[ExtensionOf(classStr(SalesInvoiceJournalPost))]
final class SLD_SalesInvoiceJournalPost_Extension
{
    /// <summary>
    ///   Performs the customer related postings.
    /// </summary>
    protected void postCustVend()
    {
        CustTrans                   custTrans;
        Name                        ecomMethodOfPayment = this.salesTable.SLDTenderTypeName;        // getting E-Com method of payment from Sales order

        next postCustVend();

        // getting the customer transaction
        custTrans                   = CustTrans::findByVoucher(this.custInvoiceJour.LedgerVoucher);
        custTrans.selectForUpdate(true);

        // updating the customer transaction
        ttsbegin;
        custTrans.SLDTenderTypeName = ecomMethodOfPayment;
        custTrans.update();
        ttscommit;
    }
}

Please feel free to contact me if you are facing any issue during implementation of this blog.


Support Faryal's Cusine


Virtual Fields Vs Computed Fields

  Virtual Field: A virtual field in D365FO is a field that doesn't have a direct representation in the database. It's a field that y...