使用AIF更新销售订单

时间:2013-11-18 13:39:03

标签: c# axapta dynamics-ax-2012 aif

我正试图通过AIF(Dynamics AX webservices)更新C#中的销售订单

我有以下代码:

    /// <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);
        }
    }

要更新的AX订单的数据来自我的模型对象SalesOrder,我尝试将其映射到AxdSalesOrder对象以传递给服务。

我一直收到以下异常:

The key field SalesId cannot be updated.

有谁知道我在这里做错了什么? : - )

提前致谢!

2 个答案:

答案 0 :(得分:1)

首先,我建议你阅读(以防万一你没看过) - &gt;如何通过AIF "Updating Data With AIF [AX 2012]"

更新AX中的记录

根据您的代码,我假设您正在进行“部分更新”,但未指定以下

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

上面显示的相同方法适用于任何记录,部分更新。在XML中,这将是这样的:

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

而且很可能你必须指定“ClearNilFieldsOnUpdate”。

enter image description here

答案 1 :(得分:0)

您需要为部分更新指定行和标题的操作。 喜欢标题

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

和Line

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