CRM 2013,复制上一个销售订单中的订单产品

时间:2015-11-17 00:05:26

标签: javascript html copy dynamics-crm

如何将最后订购的产品添加到新创建的销售订单产品中。 我从上一个订单中检索了所有产品,但我不知道如何将它们添加到当前的sales / salesOrderDetail。

我需要在javascript中执行此操作,我无法使用Csharp代码。 函数retrieveproducts()工作正常,函数retrieveProductsCallBack(data)必须在订单上创建/复制检索到的产品,但我不知道该怎么做..欢迎任何建议和/或hulp。

//代码示例

        var salesOrderID,clientId;

    //this function is used to get products from last order
    function RetrieveProducts() {
        var odataQuery;
        var oMyObject = window.dialogArguments;
        clientId = oMyObject.customerid;
        salesOrderID = oMyObject.salesorderid;

        var entitySchemaName = "SalesOrder";
        if (document.getElementById("checkID").checked == true) {
            odataQuery = "?$top=1&$select=order_details/IsPriceOverridden,order_details/IsProductOverridden,order_details/ProductId,order_details/SalesOrderId,order_details/UoMId&$orderby=CreatedOn desc&$expand=order_details&$filter=CustomerId/Id eq guid'" + clientId + "'";
        }
        else {
            odataQuery = "?$top=1&$select=order_details&$orderby=CreatedOn desc&$expand=order_details&$filter=CustomerId/Id eq guid'" + clientId + "'";
        }

        SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, retrievedProductsCallBack, function (error) { alert(error.message); }, function () { });
    }

    function retrievedProductsCallBack(data) {

        for (var i = 0; i < data[0].order_details.results.length ; i++) {
           var rec = data[0].order_details.results[i];
           var product = rec.ProductId;

           var product = {};

            //Set ohter required attrbitues of products
           product.SalesOrderId = salesOrderID;
           product.ProductId = rec.ProductId;
           product.Quantity = rec.Quantity;


            //create quote product
           SDK.REST.createRecord(product, "SalesOrderDetail", function () { }, function (error) { alert(error.message); }, function () { });
        }
    }
</script>

1 个答案:

答案 0 :(得分:0)

如果您更改

,您的代码应该有效
product.SalesOrderId = salesOrderID;

product.SalesOrderId = {Id: salesOrderID, LogicalName: "SalesOrder"};

因为SalesOrderId是实体引用。