在 dynamics crm 中将销售订单详细信息添加到销售订单

Adding sales order details to sales order in dynamics crm

我正在创建一个销售订单,将单个产品添加到销售订单详细信息并将其附加到销售订单。

它向我抛出一个错误,我想知道是否有执行此操作的正确方法?

谢谢!

 public void  Create(CrmContextCore _crmContext, Guid productId, UserEntityModel currentuser)
{
    var detail = new Entity("salesorderdetail");
    {

        detail["productid"] = new EntityReference("product", productId);
    }

    var salesorder = new Entity("salesorder");
    {
        salesorder["accountid"] = new EntityReference("account", currentuser.AccountId);
        salesorder["contactid"] = new EntityReference("contact", currentuser.ContactId );
        salesorder["emailaddress"] = currentuser.Email;
        salesorder["name"] = "DealerPO123";
        salesorder["salesorderdetail"] = detail;

    }

    _crmContext.ServiceContext.AddObject(salesorder);

    _crmContext.ServiceContext.SaveChanges();

}

Sample: Set negative prices in opportunities, quotes, and sales orders.

// Create the sales order.
SalesOrder order = new SalesOrder()
{
    Name = "Faux Order",
    DateFulfilled = new DateTime(2010, 8, 1),
    PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName,
        _priceListId),
    CustomerId = new EntityReference(Account.EntityLogicalName,
        _accountId),
    FreightAmount = new Money(20.0M)
};
_orderId = _serviceProxy.Create(order);
order.Id = _orderId;

// Add the product to the order with the price overriden with a
// negative value.
SalesOrderDetail orderDetail = new SalesOrderDetail()
{
    ProductId = new EntityReference(Product.EntityLogicalName, 
        _product1Id),
    Quantity = 4,
    SalesOrderId = order.ToEntityReference(),
    IsPriceOverridden = true,
    PricePerUnit = new Money(-40.0M),
    UoMId = new EntityReference(UoM.EntityLogicalName, 
        _defaultUnitId)
};
_orderDetailId = _serviceProxy.Create(orderDetail);