Wednesday, September 19, 2012

Dynamics Ax - Update using AIF | Get Document Hash

Generate <_DocumentHash> while updating data thru AIF

In Dynamics Ax, version 2009 onwards Document hash is mandatory to update the records using AIF.

When you read data, AIF returns a document hash field. This field contains a hash of all the RecId and RecVersion values for each record that is returned. When you send the data back into AIF to update a record, it recalculates the document hash from the database records in the update and compares it to the document hash in the inbound message. If the data has changed, for example, if a record was updated or added, the calculated document hash will differ from the document hash in the inbound document and AIF will return an error.

When you use the document hash, you only have to read and submit one field for comparison instead of reading and submitting the RecId and RecVersion values for each record. However, if a concurrency error occurs when you use the document hash, AIF can only report that a record was changed and not which record changed.

While implementing some of the scenarios we may even need to perform update in 1 step this could be only possible when are able to generate <_DocumentHash> using X++. Below code describes same.
I have created a test class which extends to AxdBaseRecordInfo and have created one method. In the method I am passing a recId for LedgerJournalTrans and it should return documenthash.

class MyHashCode  extends AxdBaseRecordInfo


public static str getHashCode(RecId     _recId)
    AxdBaseUpdate           axdBaseUpdate = AxdbaseUpdate::construct();
    Map                          dataSourceMap;
    Query                       query = new Query(QueryStr(AxdLedgerGeneralJournal));
    AxdBaseRecordInfo      topAxdBaseRecordInfo;
    QueryRun                  _queryRun;
    str                           documentHash;
    QueryBuildRange         qr;
    LedgerJournalTrans     ledgerJournalTrans;
    RecId                        recId;

    recId                       = _recId;
    select firstOnly ledgerJournalTrans
        where ledgerJournalTrans.RecId == recId;

    dataSourceMap               = new Map(Types::Integer,Types::Integer);
    axdbaseUpdate.buildDataSourceParentMap(dataSourceMap, query.dataSourceNo(1)) ;
    query.dataSourceNo(1).addRange(fieldnum(LedgerJournalTrans, RecId)).value(queryValue(recId));

    topAxdBaseRecordInfo        = new AxdBaseRecordInfo(ledgerJournalTrans,1,dataSourceMap);
    documentHash                = topAxdBaseRecordInfo.getRecordHash();

    return documentHash;


static void generateDocHash(Args _args)


Above code has been designed to support AX 2012 changes, if you are looking how to perform same in AX 2009 then follow :

Happy DAXing :)


  1. AIF: how to update the operation (AifRecordAction) from create to update when the document entity key already exists in table. Eg, an inbound XML document for a customer has specified 'create' operation, but if the customer exists, I wld like to perform update operation. Any suggestion !

    1. Hy Dipankar,

      You will have to perform two way operation in this case. In first call you will pass Customer Id for FIND service operation, if you get 0 record then u could call create operation else not.