Update order header through AIF

The following code sample shows how to update an existing order through AIF from outside Dynamics AX 2012 . Scenario is the we need to update some of the fields at header of Order.

 public static int UpdatePurchaseOrder(string _purchId, CallContext CallContextObj = null)  
         KeyField keyField = new KeyField() { Field = "PurchId", Value = _purchId };  
         EntityKey entityKey = new EntityKey();  
         entityKey.KeyData = new KeyField[1] { keyField };  
         EntityKey[] entityKeys = new EntityKey[1] { entityKey };  
         if (CallContextObj == null)  
           CallContextObj = CallContextSettings.getCallContextObject();  
         using (ANPurchOrderServiceClient client = new ANPurchOrderServiceClient())  
           client.ClientCredentials.Windows.ClientCredential.Domain = CredentialsSettings.Domain;  
           client.ClientCredentials.Windows.ClientCredential.UserName = CredentialsSettings.UserName;  
           client.ClientCredentials.Windows.ClientCredential.Password = CredentialsSettings.Password;  
           AxdANPurchOrder PurchaseOrderDoc = client.read(null, entityKeys);  
           AxdEntity_PurchTable purchTable = PurchaseOrderDoc.PurchTable.First();  
           purchTable.ANPublishedState = AxdEnum_ANPublishedState.Published;  
           purchTable.ANPublishedStateSpecified = true;  
           purchTable.action = AxdEnum_AxdEntityAction.update;  
           purchTable.actionSpecified = true;  
           purchTable.PurchLine = null;  
           // Invoke the update operation  
           AxdANPurchOrder newOrder = new AxdANPurchOrder()  
             PurchTable = new[] { purchTable }  
           client.update(CallContextObj, entityKeys, newOrder);  
           return 1;  
       catch (Exception ex)  
       return 0;  

With above code we did as per need, read the purchase order document through read operation, set some fields and last set actions also. when I try to test the application I got some funny strange error

“Invalid entity action.”

This error confuse me also, as I have set the action also. After some research I came to know that read operation of service is returning both header and lines also, which I don’t need that for my scenario. So the fix is that I need to set the line object to null also and then send for update operation.

purchTable.PurchLine = null;

Question after this fix came In my mind is that can we only read header or specific line through read operation ? I will research about this more. If anyone knows this please share your thoughts with me. THANKS


One Response to Update order header through AIF

  1. Adeel Ahmad says:

    Hi Amir,

    This was a good research done by you.
    To do an Update operation on a Table in AX using AIF we need to send current record Hash value in “_DocumentHash” attribute of incoming message, so if you are making a selection of fields for this AIF service, always make sure that this attribute is selected/ticked in AIF service data policies. But in your above mentioned case, as you are reading current data into an object first and then sending it back after updating required fields, that’s why you are not getting any errors. Which is good. I normally don’t use read operation for an update task and handle it in AxdClass.

    When we use Read operation with AIF service, we always use all fields in EntityKey object, which are present in Primary Index of the top most table in the AxdQuery and then AX will handle rest of the chain of tables in AxdQuery. So if you want to read/update a particular PurchLine record, you have to create another AxdQuery with PurchLine at top and use “InventTransId” field as EntityKey object, you will update only the required record in lines.

    Please correct me, if i am wrong OR if we can call a particular PurchLine using PurchId itself.

    many thanks.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: