Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

ParentEntity.RelatedEntities.Add(Childentites) is not working all the time. Am I missing something?

Posted on by 45

I am updating child entities in the pre-operation of Update message. 

Parent Entity - salesorder

Child entity - salesorderdetail

code snippet here:

List lineEntitesToUpdate = new List();

foreach (Entity lineEntity in ec.Entities)

{

    lineEntity["tax"] = new Money(9.5);

    //other custom settings update

    lineEntitiesToUpdate.Add(lineEntity);

}

orderFromContext.RelatedEntities.Add(new Relationship("order_details"), new EntityCollection(lineEntitiesToUpdate));

orderFromContext is the entity obtained from context. 

ec is the entitycollection of related salesorderdetail

Reason i am not using service.Update(lineEntity); is because when 1st line item is updated that calls parent update and it messes up rest of the line items

Problem:

Last line in the code does update the related child entities only once, subsequent updates it wont change anything for child entities. I even don't know why it updates only once and when it decides to update once. There no pattern to it that lines are updated only after creation on line items, it seems random. I don't know why it update only once and not subsequent once. 

note: I am not using service.Update(orderFromContext); because my plugin is registered on pre-operation of Update

Please let me know if you need any more information. I followed this link for relatedentities https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/org-service/entity-operations-update-delete

  • newtomsworld Profile Picture
    newtomsworld 45 on at
    RE: ParentEntity.RelatedEntities.Add(Childentites) is not working all the time. Am I missing something?

    Yes I had tried that too, but as i said it only works 1 time and stops working for subsequent updates. 

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: ParentEntity.RelatedEntities.Add(Childentites) is not working all the time. Am I missing something?

    Hi,

    Can you please update your code as below and check.

    List lineEntitesToUpdate = new List();
    
    foreach (Entity lineEntity in ec.Entities)
    
    {
        Entity salesOrder=new Entity("{Replace with Child Entity Name logical name}");
        salesOrder.Id=lineEntity.Id;
        salesOrder["tax"] = new Money(9.5);
    
        //other custom settings update
    
        lineEntitiesToUpdate.Add(salesOrder);
    
    }
    
    orderFromContext.RelatedEntities.Add(new Relationship("order_details"), new EntityCollection(lineEntitiesToUpdate));

    Please mark my answer verified if i were helpful

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: ParentEntity.RelatedEntities.Add(Childentites) is not working all the time. Am I missing something?

    No duplicates, please.

  • newtomsworld Profile Picture
    newtomsworld 45 on at
    ParentEntity.RelatedEntities.Add(Childentites) is not working all the time. Am I missing something?

    I am updating child entities in the pre-operation of Update message. 

    Parent Entity - salesorder

    Child entity - salesorderdetail

    code snippet here:

    List lineEntitesToUpdate = new List();
    
    foreach (Entity lineEntity in ec.Entities)
    
    {
    
        lineEntity["tax"] = new Money(9.5);
    
        //other custom settings update
    
        lineEntitiesToUpdate.Add(lineEntity);
    
    }
    
    orderFromContext.RelatedEntities.Add(new Relationship("order_details"), new EntityCollection(lineEntitiesToUpdate));

    orderFromContext is the entity obtained from context. 

    ec is the entitycollection of related salesorderdetail

    Reason i am not using service.Update(lineEntity); is because when 1st line item is updated that calls parent update and it messes up rest of the line items

    Problem:

    Last line in the code does update the related child entities only once, subsequent updates it wont change anything for child entities. I even don't know why it updates only once and when it decides to update once. There no pattern to it that lines are updated only after creation on line items, it seems random. I don't know why it update only once and not subsequent once. 

    note: I am not using service.Update(orderFromContext); because my plugin is registered on pre-operation of Update

    Please let me know if you need any more information. I followed this link for relatedentities https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/org-service/entity-operations-update-delete

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

Product updates

Dynamics 365 release plans