bomortensen
bomortensen

Reputation: 3396

Updating a salesorder using AIF

I'm running my head against the wall here trying to update a salesorder in C# through AIF (Dynamics AX webservices)

I have the following code:

    /// <summary>
    /// Updates an order in AX
    /// </summary>
    /// <param name="order">Order to update</param>
    public void UpdateOrder(SalesOrder order)
    {
        try
        {
            var client = new write.SalesOrderServiceClient();
            var callContext = new write.CallContext() { Company = "mcompany" };
            var entityKeyList = new write.EntityKey[1];
            var entityKey = new write.EntityKey();
            var keyField = new write.KeyField();
            keyField.Field = "SalesId";
            keyField.Value = order.orderCaseId;                
            entityKey.KeyData = new write.KeyField[1] { keyField };
            entityKeyList = new write.EntityKey[1] { entityKey };

            var salesOrder = new write.AxdSalesOrder
            {
                DocPurpose = write.AxdEnum_XMLDocPurpose.Original,
                SalesTable = new write.AxdEntity_SalesTable[1],
            };


            salesOrder.SalesTable[0] = new write.AxdEntity_SalesTable
            {
                PurchOrderFormNum = order.purchaseOrderFormNumber,
                ReceiptDateRequested = order.receiptDateRequested,
                SalesLine =
                    new write.AxdEntity_SalesLine[order.salesOrderLines.Count()],
                _DocumentHash = order.documentHash
            };

            var orderLinesArray = order.salesOrderLines.ToArray();
            for (int i = 0; i < order.salesOrderLines.Count(); i++)
            {
                salesOrder.SalesTable[0].SalesLine[i] = new write.AxdEntity_SalesLine()
                {
                    SalesQty = orderLinesArray[i].quantity,
                    SalesUnit = orderLinesArray[i].unit,
                };
            }

            client.update(callContext, entityKeyList, salesOrder);
        }
        catch (Exception e)
        {
            Logging.AddLogEntry(e.Message);
        }
    }

The data for the AX order to be updated comes from my model object SalesOrder which I try to map to an AxdSalesOrder object to pass to the service.

I keep getting the following exception:

The key field SalesId cannot be updated.

Does anyone know what I'm doing wrong here? :-)

Thanks in advance!

Upvotes: 0

Views: 4731

Answers (2)

user2346945
user2346945

Reputation: 1

you need to specify action for both line and header for partial updation. Like for header

salesTable.action = AxdEnum_AxdEntityAction.update; 
salesTable.actionSpecified = true; 

and for Line

salesLine.action = AxdEnum_AxdEntityAction.create; 
salesLine.actionSpecified = true;

Upvotes: 0

Yuriy
Yuriy

Reputation: 84

First of all I suggest you to read (just in case you didn't read it) -> how to update records in AX via AIF "Updating Data With AIF [AX 2012]"

Based on your code I assume that you're doing "partial update", but you didn't specified following

//here you need to specify for EACH record what you need, i.e. "create/update/delete"
salesLine.action = AxdEnum_AxdEntityAction.update; 
salesLine.actionSpecified = true;

The same approach shown above applied to any record, which is to be partially updated. In XML this will look like this:

<SalesLine class="entity" action="update">

And most likely you would have to specify "ClearNilFieldsOnUpdate".

enter image description here

Upvotes: 1

Related Questions