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)
{
try
{
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)
{
Console.WriteLine(ex.ToString());
}
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