在Dynamics CRM中将销售订单详细信息添加到销售订单

时间:2018-10-27 17:05:27

标签: c# .net .net-core dynamics-crm xrm

我正在创建一个销售订单,将单个产品添加到销售订单明细中并将其附加到该销售订单上。

这使我犯了一个错误,我想知道是否存在执行此操作的正确方法?

谢谢!

 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();

}

1 个答案:

答案 0 :(得分:0)

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