Sales order Cancellation using AIF in dynamics ax 2009

i want to cancel the order from .net using AIF service in dunamics ax 2009.

////// here is my code

this code is not working, throwing exception : “The key field SalesId cannot be updated”


public void salesorderCancel()


SalesOrderServiceClient salesService = new SalesOrderServiceClient();

AxdSalesOrder axdsales = new AxdSalesOrder();

AxdEntity_SalesTable salesTable = new AxdEntity_SalesTable();

AxdEntity_SalesLine[] SalesLine = new AxdEntity_SalesLine[10];

EntityKey key = new EntityKey();

EntityKey[] keys = new EntityKey[1];

KeyField fld = new KeyField();

fld.Field = “SalesId”;

fld.Value = “00000342_032”;

key.KeyData = new KeyField[] { fld };

keys = new EntityKey[] { key };

AxdSalesOrder TempSalesOrderObj =;

AxdEntity_SalesTable[] TempSalesTable = TempSalesOrderObj.SalesTable;

salesTable.ReceiptDateRequested = TempSalesTable[0].ReceiptDateRequested;

salesTable.PurchOrderFormNum = “1”;

salesTable._DocumentHash = TempSalesTable[0]._DocumentHash;

salesTable.action = AxdEnum_AxdEntityAction.update;

salesTable.actionSpecified = true;

SalesLine[0] = new AxdEntity_SalesLine();

SalesLine[0].actionSpecified = true;

SalesLine[0].action = AxdEnum_AxdEntityAction.update;

SalesLine[0].RecIdSpecified = true;

SalesLine[0].RecId = TempSalesTable[0].SalesLine[0].RecId;

SalesLine[0].RecVersionSpecified = true;

SalesLine[0].RecVersion = TempSalesTable[0].SalesLine[0].RecVersion;

SalesLine[0].ItemId = “T0063”;

//SalesLine[0].SalesQty = Convert.ToDecimal(“4”);

SalesLine[0].SalesUnit = “EACH”;

SalesLine[0].RemainInventPhysicalSpecified = true;

SalesLine[0].RemainInventPhysical = Convert.ToDecimal(“0”);

SalesLine[0].RemainSalesPhysicalSpecified = true;

SalesLine[0].RemainSalesPhysical = Convert.ToDecimal(“0”);

salesTable.SalesLine = new AxdEntity_SalesLine[] { SalesLine[0] };

axdsales.SalesTable = new AxdEntity_SalesTable[] { salesTable };

salesService.update(keys, axdsales);

// salesLine.RemainInventPhysical = 0;

// salesLine.RemainSalesPhysicalSpecified = true;

// salesLine.RemainSalesPhysical = 0;

// salesTable.SalesLine = new AxdEntity_SalesLine[]{ salesLine };

// axdsales.SalesTable = new AxdEntity_SalesTable[] { salesTable };

// salesService.update(call, keys, axdsales);

//// salesService.update(call, AxEntityKey, axdsales);


please help me out.


Why do you try writing to fields such as _DocumentHash, RecId or RecVersion? It doesn’t sound as a good idea. What did you want to achieve by that? Why are you trying to create new AxdEntity_SalesTable and AxdEntity_SalesLine instead of updating those you got from read()?

i also tried updating AxdEntity_SalesLine that i got from read() but this is also not working.


public void SalesOrderCancellation()


AXSalesIntegrationRef.SalesOrderServiceClient salesService = new SalesOrderServiceClient();

AxdSalesOrder axdsales = new AxdSalesOrder();

EntityKey[] AxEntityKey = new EntityKey[1];

AxEntityKey[0] = new EntityKey();

KeyField[] AxKeyField = new KeyField[1];

AxKeyField[0] = new KeyField();

AxKeyField[0].Field = “SalesID”;

AxKeyField[0].Value = “00000306_032”;

AxEntityKey[0].KeyData = AxKeyField;

axdsales =;

axdsales.SalesTable[0].SalesLine[0].RemainInventPhysicalSpecified = true;

axdsales.SalesTable[0].SalesLine[0].RemainInventPhysical = 0;

axdsales.SalesTable[0].SalesLine[0].RemainSalesPhysicalSpecified = true;

axdsales.SalesTable[0].SalesLine[0].RemainSalesPhysical = 0;

salesService.update(AxEntityKey, axdsales);


What “isn’t working” exactly means? You are unable to update the order at all? Do you get any error in that case? Did you review AIF logs?

it is not giving me any error. when i try to update any other field(eg . salesqty), it is getting updated.

but when i try to update “remaininventphysical” and “remainsalesphysical” fields for sales order cancellation, the fields are not getting updated and also status of sales order is not getting changed. it remains as “open order”.

Use debugger to see what exactly happens in code. See Debugging AIF Web Services.