Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Cannot delete a write in product in SalesOrder, which was created and trying to delete programmatically. "salesorderdetail With Id Does Not Exist"

Posted on by 45

In salesorder entity, I create a write-in line item if I have freight amount. The Line is created if there is a tax on freight. If the freight amount is completely taken out then I want to delete that item. But I get the following error "salesorderdetail With Id = c471fe88-a3cd-e811-a973-000d3a1a9407 Does Not Exist"

In fact the line Id does exists with same id in the db.

Here is the code to insert the line in the salesorder 

Guid freightDetailId = new Guid();
Entity freightLine = new Entity("salesorderdetail", freightDetailId);
freightLine["salesorderid"] = new EntityReference("salesorder", gEntityId);
freightLine["isproductoverridden"] = true;
freightLine["priceperunit"] = new Money(0);
freightLine["quantity"] = Convert.ToDecimal(1);
freightLine["productname"] = "Freight Tax line";
freightLine["productdescription"] = "Freight Tax line";
freightLine["tax"] = new Money(10.00);
service.Create(freightLine);


Here is the code I use to delete the same line from salesorderdetail if freight is either 0 or not present at all

if (freightLineExists) // if freight line exists then delete it as freight is not available anymore
{
     tracer.Trace("freight exists");
     tracer.Trace("Freight ID = "+ ec.Entities[freightLineNumber].Id);
     service.Delete(ec.Entities[freightLineNumber].LogicalName, ec.Entities[freightLineNumber].Id);
}

*This post is locked for comments

  • newtomsworld Profile Picture
    newtomsworld 45 on at
    RE: Cannot delete a write in product in SalesOrder, which was created and trying to delete programmatically. "salesorderdetail With Id Does Not Exist"

    Here is my code

    QueryExpression query = new QueryExpression("salesorderdetail");

    query.ColumnSet.AllColumns = true;

    query.Criteria.AddCondition("salesorderid", ConditionOperator.Equal, entityId);

    EntityCollection ec = service.RetrieveMultiple(query);

    I think I have figured out why. Its solved now.

    I have service.update(entity) in multiple places which updated other entites in the "ec". and once that is done there is data.refresh on clinet side which triggers save. I think this double saving may have caused it. I now removed all

    service.Update(ec.Entities[i]); and it solved the problem

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Cannot delete a write in product in SalesOrder, which was created and trying to delete programmatically. "salesorderdetail With Id Does Not Exist"

    Hello,

    Can you please provide code how to do you retrieve ec - from line ec.Entities[freightLineNumber].Id

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans