locked
How to use the Entity class for creating Orders but also add the products on the order???!!! RRS feed

  • Question

  • Hi Everyone, I manage to create a program that can create an order using the link provided below.

    While that is fine, the challenge comes from the new_ordernumber field in the Salesorder entity.

    This gets automatically generated on the backend, and I'm not sure how I can reference that value to

    be used to create a product via the salesorderdetail entity. I provided the code below, but I'm not sure

    how to reference the new_ordernumber from the salesorder entity and use it to create a new record in the

    salesorderdetail entity. Can someone please help? Thanks!

    Reference: https://msdn.microsoft.com/en-in/library/gg309731.aspx

    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. // See the Entity Metadata topic in the SDK documentatio to determine // which attributes must be set for each entity. salesorder["name"] = r.Name.ToString(); salesorder["new_ordernumber"] = // Create an salesorder record named "Test"; _orderId = _service.Create(salesorder); Console.Write("{0} {1} {2} created, ", salesorder.LogicalName, salesorder.Attributes["name"], salesorder.Attributes["new_ordernumber"]); // Create a column set to define which attributes should be retrieved. ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid" }); // Retrieve the salesorder and its name and ownerid attributes. salesorder = _service.Retrieve(salesorder.LogicalName, _orderId, attributes); Console.Write("retrieved, "); // Update the customer attribute. salesorder["new_promotioncode"] = "TEST CODE"; // Update the salesorder. _service.Update(salesorder); Console.WriteLine("and updated."); // Delete the salesorder. bool deleteRecords = true; if (promptForDelete) { Console.WriteLine("\nDo you want these entity records deleted? (y/n) [y]: "); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y") || answer == String.Empty); } if (deleteRecords) { _service.Delete("salesorder", _orderId); Console.WriteLine("Entity record(s) have been deleted."); } } } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } } } }


    Sunday, November 22, 2015 4:27 PM

Answers

  • If you need the new_ordernumber attribute value, then you will need to include it in the attributes ColumnSet:

    ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid", "new_ordernumber" });
    
                            // Retrieve the salesorder and its name and ownerid attributes.
                            salesorder = _service.Retrieve(salesorder.LogicalName, _orderId, attributes);
                            Console.Write((string)salesorder["new_ordernumber"]);

    However, do you need this attributes in the salesorderdetail ? The link between the salesorderdetail and salesorder is via the salesorderid that you've already got in _orderId


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by tekwise1 Wednesday, November 25, 2015 1:22 AM
    Monday, November 23, 2015 9:39 AM
    Moderator
  • After creating the salesorder you can create the SalesOrderDetail.

    What you need to know is that the detail is linked to the principal entity by the Guid (the salesoderid field)that is represented as the Id of the record and not by the orderNumber.

    Guid  _orderId = _service.Create(salesorder);
    Entity salesOrderDetail = new entity("SalesOrderDetail");
    
    salesOrderDetail.Attributes["salesorderid"] =  new EntityReference("salesorder", _orderId);
     _service.Create(salesOrderDetail );
    
    Have a good day.


    • Proposed as answer by saad nadir Monday, November 23, 2015 12:09 PM
    • Marked as answer by tekwise1 Wednesday, November 25, 2015 1:22 AM
    Monday, November 23, 2015 11:27 AM

All replies

  • If you need the new_ordernumber attribute value, then you will need to include it in the attributes ColumnSet:

    ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid", "new_ordernumber" });
    
                            // Retrieve the salesorder and its name and ownerid attributes.
                            salesorder = _service.Retrieve(salesorder.LogicalName, _orderId, attributes);
                            Console.Write((string)salesorder["new_ordernumber"]);

    However, do you need this attributes in the salesorderdetail ? The link between the salesorderdetail and salesorder is via the salesorderid that you've already got in _orderId


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by tekwise1 Wednesday, November 25, 2015 1:22 AM
    Monday, November 23, 2015 9:39 AM
    Moderator
  • After creating the salesorder you can create the SalesOrderDetail.

    What you need to know is that the detail is linked to the principal entity by the Guid (the salesoderid field)that is represented as the Id of the record and not by the orderNumber.

    Guid  _orderId = _service.Create(salesorder);
    Entity salesOrderDetail = new entity("SalesOrderDetail");
    
    salesOrderDetail.Attributes["salesorderid"] =  new EntityReference("salesorder", _orderId);
     _service.Create(salesOrderDetail );
    
    Have a good day.


    • Proposed as answer by saad nadir Monday, November 23, 2015 12:09 PM
    • Marked as answer by tekwise1 Wednesday, November 25, 2015 1:22 AM
    Monday, November 23, 2015 11:27 AM