Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Product with specified unit does not exists in price list???

Posted on by Microsoft Employee

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

  • Aileen Gusni Profile Picture
    Aileen Gusni 44,522 on at
    RE: Product with specified unit does not exists in price list???

    Hi Tekwise

    Wow..

    Nice..

    Good for you!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Product with specified unit does not exists in price list???

    Figured it out...You were right Aileen, it was pulling the wrong uomid. I was pulling the name of the Unit group and not the individual value for look up.

    Thanks for helping me along the way! You're definitely a star!

    var uom = "Pair";   and not var uom = "Default Unit";  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Product with specified unit does not exists in price list???

    I just found this: https://msdn.microsoft.com/en-us/library/dn817877.aspx

    Does this mean I have to create code to calculate the price for a salesorderdetail for each line item added to the order?? Ouch....

    Actually I also found this:

    http://question.ikende.com/question/31373639393334343831

    How do I associate the product in the price list using C# code? What's the syntax? Anyone?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Product with specified unit does not exists in price list???

    [quote user="Aileen Gusni"]Hi Tekwise

    Seems like you need to use Pair instead of Default Unit in your code.

    It is based on your picture.

    Can try?

    Thanks

    [/quote]

    Hi Aileen, it is using pair. The default unit is whats loaded first and then changes by itself when the product detail order form opens up.

    Am I missing any fields to populate when I create the salesorderdetail?

    • existing product
    • order id (it pulls off the existing order)
    • qty
    • oum

    i believe is what my code to create the sales order detail line item. Also, the uom is correct because I compared the GUID from the front end verses the code. I think the real issue is why my code cannot do the calculation for price when a new salesorderdetail is created. 

  • Verified answer
    Aileen Gusni Profile Picture
    Aileen Gusni 44,522 on at
    RE: Product with specified unit does not exists in price list???

    Hi Tekwise

    Seems like you need to use Pair instead of Default Unit in your code.

    It is based on your picture.

    Can try?

    Thanks

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Product with specified unit does not exists in price list???

    [quote user="Aileen Gusni"]

    Hi Tekwise

    No.

    Lookup to product is correct

    Thats why I ask you to try manually created is that working.

    And then pls make sure that not only the product but product with that specific unit in the price list.

    And pls check the uomid is thst correct id.

    Thanks

    [/quote]

    Hi Aileen,

    I took snapshots for you to see the situation...let me know your thoughts on what you think is happening here.

    As you can see, the error occurs once the product item is added to the order via C# SDK code. You can see that the price is zero but has a quantity of 7. The price list for this order is Default Price List.

    Product-Info.png

    I then open up the Order Detail page and it's all zero'd out (default values) for a few seconds and then automatically calculates the total when I wait for the page to finish loading.

    beforeOrderDetail.png

    Now that the price is calculated automatically, if I hit save from here the error message is gone and problem fixed. But we want don't want to manually save this for every order we created automatically using C# SDK code.

    afterOrderDetail.png

    More Information:

    Here's the product information and you can see the default unit is Pair and default price list is Default Price List.

    Product-Info.png

    Here's the price list line item info from the Default Price List, you can see the default unit is Pair.

    ProductPriceListItems.png

  • Aileen Gusni Profile Picture
    Aileen Gusni 44,522 on at
    RE: Product with specified unit does not exists in price list???

    Hi Tekwise

    No.

    Lookup to product is correct

    Thats why I ask you to try manually created is that working.

    And then pls make sure that not only the product but product with that specific unit in the price list.

    And pls check the uomid is thst correct id.

    Thanks

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Product with specified unit does not exists in price list???

    Hi Aileen, 

    I do see the product in the price list and the default price list is already selected for the order.

    Instead of looking up the product from the PRODUCT entity, are you suggesting I look it up in the productpricelevel ENTITY instead?

  • Aileen Gusni Profile Picture
    Aileen Gusni 44,522 on at
    RE: Product with specified unit does not exists in price list???

    Hi Tekwise

    If the amount is 0 it is something thst caused by price list thing.

    I encountered before.

    Because crm cannot find this product with this specific unit how much? In the quote or oder you have the price list right but crm cant find it.

    You need to either put this product with really specific unit in the price list item. This is very important.

    Or you can just create a default price list to make this crm calculate the pricing correctly based on the default.

    Previously I was replied here before

    community.dynamics.com/.../144356

    community.dynamics.com/.../143114

    You really need the price list item for the specific product and the price list must be same with the price list you chosen to create the quote or order

    Otherwise just pls use the default price list to "enforce" crm to to his job to calculat the amount

    missdynamicscrm.blogspot.sg/.../10-facts-about-default-price-list-in-CRM2011-2013-that-you-might-not-have-noticed.html

    Hope this helps.

    Thanks

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Product with specified unit does not exists in price list???

    Hi Aileen, thanks so much for replying!

    Yes, the product exists. The strange thing is if I open the product detail form from the order, the total amount calculates and when I save it, the error goes away.

    If I just go right into it from the order form, the total amount is zero and immediately calculates. So something is not working...thoughts?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans