I was able to create a OrderDetail entity in CRM in C#. However, when I try to add a product line item to the order detail entity, I get the following error:
"The following error occurred: Product with specified unit does not exists in price list."
I looked in the price list and the product is there and also the unit group and the unit measure is correct.
Here's my code:
#region Class Level Members
private OrganizationServiceProxy _serviceProxy;
private IOrganizationService _service;
// Define the IDs needed for this sample.
private Guid _orderId;
#endregion Class Level Members
#region ImportToCRM - Create New Order
public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete)
{
try
{
// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
{
//late bound
#region XML
XNamespace ns = "www.test-inc.com";
XDocument document = XDocument.Load(@"E:\\Orders.xml");
var orders = (from r in document.Descendants(ns + "OrderHeader")
select new
{
PartnerId = r.Element(ns + "PartnerId").Value,
PurchaseOrderNumber = r.Element(ns + "PurchaseOrderNumber").Value,
Name = r.Element(ns + "Name").Value,
customerid = r.Element(ns + "customerid").Value,
ProductName = r.Element(ns + "ProductName").Value,
ProductNumber = r.Element(ns + "ProductNumber").Value
}).ToList();
#endregion XML
#region CRM Create Orders
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["neu_promotioncode"] = "Test Promo";
//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)
var pricelistname = "Default Price List";
var pricelist = GetEntityCollection(_service, "pricelevel", "name", pricelistname, new ColumnSet("pricelevelid", "name"));
Guid pricelevelid = (Guid)pricelist[0].Id;
salesorder["pricelevelid"] = new EntityReference("pricelevel", new Guid(pricelevelid.ToString()));
salesorder.Id = _orderId; // the order id is mapped to the specific order, can be used to map the product detail entity (salesorderdetail)
// Create an salesorder record
_orderId = _service.Create(salesorder);
Console.WriteLine("This is the orderid: " + _orderId.ToString());
Console.WriteLine("Sales Order created, ");
#endregion CRM Create Orders
#region Comment
#endregion Comment
#region SalesOrderDetail
Entity salesorderdetail = new Entity("salesorderdetail");
var uom = "Default Unit";
//Query for the GUID of the UnitofMeasure using Name
QueryExpression queryUOM = new QueryExpression("uom");
string[] cols = { "uomid", "name" };
queryUOM.Criteria = new FilterExpression();
queryUOM.Criteria.AddCondition("name", ConditionOperator.Equal, uom);
queryUOM.ColumnSet = new ColumnSet(cols);
EntityCollection orderedBy = _service.RetrieveMultiple(queryUOM); //Casting the reference to GUID
Guid uomscheduleGuid = (Guid)orderedBy.Entities[0].Id;
salesorderdetail["uomid"] = new EntityReference("uom", new Guid(uomscheduleGuid.ToString()));
//Setting the quantity value
salesorderdetail["quantity"] = Convert.ToDecimal(7);
//Query for the GUID of the Contact using fullname
var prodname = "Nike Shoes";
QueryExpression queryProd = new QueryExpression("product");
string[] cols2 = { "productnumber", "name" };
queryProd.Criteria = new FilterExpression();
queryProd.Criteria.AddCondition("name", ConditionOperator.Equal, prodname);
queryProd.ColumnSet = new ColumnSet(cols2);
var prodby = _service.RetrieveMultiple(queryProd);
Guid prodGuid = (Guid)prodby[0].Id; //Casting the reference to GUID
salesorderdetail["productid"] = new EntityReference("product", new Guid(prodGuid.ToString()));
salesorderdetail["salesorderid"] = new EntityReference("salesorder", _orderId);
// Create an salesorder record
_orderId = _service.Create(salesorderdetail);
Console.Write("Sales Order Detail created, ");
Console.Write("retrieved, ");
// Update the salesorder.
//_service.Update(salesorder);
Console.WriteLine("and updated.");
#endregion Sales Order Detail
#region Delete Orders
// 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.");
}
#endregion Delete Ordes
}
}
}
// 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;
}
}
#endregion ImportToCRM - Create New Order
#region QueryExpression Shortcut
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);
}
}
Also, when I try to delete the order, it says order id is invalid? Did the _orderid change when I created the salesorderdetail?
Should I put the GUID of the sales order in a different variable and not _orderid?
*This post is locked for comments