web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Use the Entity class to create orders in CRM - please help!!!

(0) ShareShare
ReportReport
Posted on by

Hi everyone, I'm very new to CRM C# development and need help. I was able to use the code provided in the reference link below to create a new Order (salesorder) in CRM. While that works, I am not able to get it to add a product to the same salesorder entity from this code. I believe the way the two entities relate by the ordernumber.  Can someone help me be able to add an existing product (lookup) to this new order?

Reference: msdn.microsoft.com/.../gg328416.aspx

Thank you!!

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

*This post is locked for comments

I have the same question (0)
  • ScottDurow Profile Picture
    21 on at

    You simply need to create an instance of the salesorderdetail and populate the productid entity reference attribute - [View:https://msdn.microsoft.com/en-us/library/gg327957.aspx]

  • Community Member Profile Picture
    on at

    Hi Scott, thanks for your reply. What would the syntax look like with my existing code? I'm not sure how to write the syntax that will create an  instance of the salesorderderail and populate the productid entity reference attribute based on the productid from the sales order entity that was just created (see code)?

  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    Something like:

    Entity orderDetail = new Entity("salesorderdetail");

    orderDetail["productid"] =  new EntityReference("product", product1d);

    orderDetail["quantity"] = 1;

    orderDetail["salesorderid"] = salesorder.ToEntityReference();

    orderDetail["ispriceoverridden"] = false;

    orderDetail["uomid"] =  new EntityReference("uom", defaultUnitId)

    _service.Create(orderDetail);

    Hope this helps!

  • ScottDurow Profile Picture
    21 on at

    How did you get on with this?

  • Community Member Profile Picture
    on at

    I still need help since i'm trying to clean up my code. Can you assist? The customerid is not being set for some reason and the  _service.Update(salesorder) keeps erroring out with "entity id must be specified for update"

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

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

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

    Console.WriteLine(accountId.ToString());

    Console.Write("Sales Order created, ");
     

    Console.Write("retrieved, ");

    // Update the salesorder.
      _service.Update(salesorder);

    Console.WriteLine("and updated.");

    }

    }
    }

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

  • kyleknab Profile Picture
    517 on at

    If your custom GetEntityCollection method is returning an EntityCollection object, then to get id of the first member of that collection, the syntax should be...

    Guid accountId = (Guid)account.Entities[0].Id;

  • Community Member Profile Picture
    on at

    Didnt work, the customer id still not updated. 

    Ok figured out that the _service.Update(salesorder); isn't working.

    If i place the code below BEFORE  _orderId = _service.Create(salesorder);, then it works. I wonder why the _update isnt working.

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

    5074.err.PNG

  • Verified answer
    kyleknab Profile Picture
    517 on at

    Oh...didn't see you were getting this error..."entity id must be specified for update".

    You're capturing _orderid from the .Create() message, but you still need to add _orderId to your salesorder object before you pass the salesorder object to the .Update() method.

    salesorder.Id = _orderId;

  • Community Member Profile Picture
    on at

    where is  this specified?

  • Community Member Profile Picture
    on at

    Thanks so much!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans