Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to create a salesorderdetail entity for an order with products in CRM?

(0) ShareShare
ReportReport
Posted on by

Can someone please help me with this? I need help writing the syntax from scratch.

I've spent a lot of time trying to figure this out and I'm struggling...I keep getting this error. I can only use the productid, unless I'm wrong...I would greatly appreciate the help.

0537.err2.png

*This post is locked for comments

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    It won't unless you are using proxyclasses generated by crmsvcutil.exe, As you are using late bound classes to you need to use generic Entity object only. In case interested for early bound class refer: msdn.microsoft.com/.../gg327844.aspx

    msdn.microsoft.com/.../gg328499.aspx

  • Community Member Profile Picture
    on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    When I type in SalesOrderDetail, it doesnt recognize that class.

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    Check this sample code it is for creating opportunity and opportunity product you can similarly create order and order product. https://msdn.microsoft.com/en-us/library/gg509009.aspx

    Make sure to write your code under try and catch block so that you can capture correct error message, it will help you to fix issue.

    try{

    //your code

    }

    catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)

    {

        Console.WriteLine("Message: {0}", ex.Detail.Message);

    }

  • Community Member Profile Picture
    on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    [quote user="tekwise"]

    Can someone please help me with this? I need help writing the syntax from scratch.

    I've spent a lot of time trying to figure this out and I'm struggling...I keep getting this error. I can only use the productid, unless I'm wrong...I would greatly appreciate the help.

    0537.err2.png

    [/quote]

    Hi Maheen, I'm still having issues after assigning guid to the salesorder id.

    It fails at this step:   var orderProduct = GetEntityCollection(_service, "salesorderdetail", "productid", r.ProductNumber, new ColumnSet("salesorderdetailid", "productid"));

    You mention of examples of how to create a product order detail that links to a sales order...can you point me in that direction? I think I'm really close, but just this last piece. I see that it takes the salesorder id and trying to assign it as a guid. It's not taking the guid of the sales order. Thank you

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    Yes, in sales order details, you need to fill salesorderid lookup to associate your salesorderdetails with particular sales order.

  • Community Member Profile Picture
    on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    [quote user="HIMBAP"]

    It seems you have not written code correctly

    salesorder.Id = _orderId; // the order id is mapped to the specific order, can be used to map the product detail entity (salesorderdetail)

    ---What is _orderId ??, Not sure what you are doing here from where you are getting it. , but it is not a good practice to assign GUID for new record, you should let CRM setup GUID.

    //SALES ORDER DETAIL

    orderDetail["salesorderdetailid"] = salesorder.Id;

    -- Why are you setting salesorderid to salesorderdetailsid ?? I will suggest you to look CRM SDK for sample code.

    instead you should set salesorderid like below

    orderDetail["salesorderid"] = salesorder.Id;

    [/quote]

    Hi Maheen,

    This was the line omitted from the code. I only captured everything inside the for each statement.

    private Guid _orderId;  

    I was trying to make sure the salesorderdetail is linked to the same sales order that was created for it at the top part of the code.  So you are suggesting that if I set the salesorderid = salesorder.id for the orderdetail it would link the salesorderdetail to the same order entity that was created in the first part of my code?

    orderDetail["salesorderid"] = salesorder.Id;

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    It seems you have not written code correctly

    salesorder.Id = _orderId; // the order id is mapped to the specific order, can be used to map the product detail entity (salesorderdetail)

    ---What is _orderId ??, Not sure what you are doing here from where you are getting it. , but it is not a good practice to assign GUID for new record, you should let CRM setup GUID.

    //SALES ORDER DETAIL

    orderDetail["salesorderdetailid"] = salesorder.Id;

    -- Why are you setting salesorderid to salesorderdetailsid ?? I will suggest you to look CRM SDK for sample code.

    instead you should set salesorderid like below

    orderDetail["salesorderid"] = salesorder.Id;

  • Community Member Profile Picture
    on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    [quote user="tekwise"]

    Can someone please help me with this? I need help writing the syntax from scratch.

    I've spent a lot of time trying to figure this out and I'm struggling...I keep getting this error. I can only use the productid, unless I'm wrong...I would greatly appreciate the help.

    0537.err2.png

    [/quote]

    Here's the code - the SALESORDER works fine - just attaching the product to the SALESORDERDETAIL ENTITY

    ========================================================

    using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
    {

    foreach (var r in orders)
    {
    _service = (IOrganizationService)_serviceProxy;

    // Instaniate an salesorder object.
    Entity salesorder = new Entity("salesorder");

    // Set the required attributes. For salesorder, only the name is required.
    salesorder["name"] = r.Name.ToString();

    //update orders with the values
    salesorder["promocode"] = "Promo";

    //CREATE NEW SALES ORDER
    //Entity Reference: set the attributes to be retrieved
    var account = GetEntityCollection(_service, "account", "name", r.customerid, new ColumnSet("accountid", "name"));
    Guid accountId = (Guid)account[0].Id;
    salesorder["customerid"] = new EntityReference("account", new Guid(accountId.ToString()));
    salesorder.Id = _orderId; // the order id is mapped to the specific order, can be used to map the product detail entity (salesorderdetail)

    //SALES ORDER DETAIL: NOT WORKING

    Entity orderDetail = new Entity("salesorderdetail");
    var orderProduct = GetEntityCollection(_service, "salesorderdetail", "productid", r.ProductNumber, new ColumnSet("salesorderdetailid", "productid"));
    Guid productOid = (Guid)orderProduct[0].Id;
    orderDetail["salesorderdetailid"] = new EntityReference("salesorderdetail", new Guid(productOid.ToString()));
    orderDetail["salesorderdetailid"] = salesorder.Id;

    // Create an salesorder record
    _orderId = _service.Create(salesorder);

    _service.Create(orderDetail);

     }

    private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
    {
    QueryExpression query = new QueryExpression
    {
    EntityName = entityName,
    ColumnSet = cols,
    Criteria = new FilterExpression
    {
    Conditions =
    {
    new ConditionExpression
    {
    AttributeName = attributeName,
    Operator = ConditionOperator.Equal,
    Values = { attributeValue }
    }
    }
    }
    };
    return service.RetrieveMultiple(query);
    }

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    It will be better if you will paste your code here so that someone can have look and can point you where is the issue, you are not setting correct ID value. Share you code

  • Community Member Profile Picture
    on at
    RE: How to create a salesorderdetail entity for an order with products in CRM?

    [quote user="prashant@dynamicscrm"]

    Hi tekwise,

    i hope this will help you, but first you have to create earlybond classes then use below code, Please let me know if you want more information.

    // Add the product to the order with the price overriden with a

               // negative value.

               SalesOrderDetail orderDetail = new SalesOrderDetail()

               {

                   ProductId = newProduct.ToEntityReference(),

                   Quantity = 4,

                   SalesOrderId = order.ToEntityReference(),

                   IsPriceOverridden = true,

                   PricePerUnit = new Money(1000.0M),

                   UoMId = new EntityReference(UoM.EntityLogicalName, _defaultUnitId)

               };

               _orderDetailId = _serviceProxy.Create(orderDetail);

    [/quote]

    Hi - I'm not sure if I understand correctly. So I just put this inside my existing code? Can you please provide complete example so I can test? Thank you

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans