Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Update Child Records in Plugin

Posted on by 445

Hello Experts,

i have a problem, i try to retrieve Child Records to Update some Attributes but i dont know how to do that.

I read so many Questions and Anwers, blogs and so on but i allways get an Error...

Can anyone help me?

                Guid seminarID = Guid.Empty;
                EntityReference seminarortId = null;

                EntityReference aref = seminarRef.Get<EntityReference>(executionContext);


                // Abrufschein ermittlen
                Entity seminar = service.Retrieve(aref.LogicalName, aref.Id, new ColumnSet("mrd_seminarort"));
                seminarID = seminar.GetAttributeValue<Guid>("mrd_seminarid");
                seminarortId = seminar.GetAttributeValue<EntityReference>("mrd_seminarort");


                //Teilnehmer ermitteln
                Entity teilnehmer = new Entity();
                QueryExpression query = new QueryExpression("mrd_teilnehmer");
                query.ColumnSet = new ColumnSet(true);
                                
                query.Criteria.AddCondition("mrd_ak_seminarid", ConditionOperator.Equal, seminarID);

                // query ausführen
                EntityCollection retrieveedxpan = service.RetrieveMultiple(query);

                tracingService.Trace(Convert.ToString(retrieveedxpan.Entities.Count));

                if (retrieveedxpan.Entities.Count == 0)
                {
                    // Template nicht gefunden
                    tracingService.Trace("Es wurden keine Teilnehmer gefunden!");
                }
                else
                {
                    
                    foreach (var entity in retrieveedxpan.Entities)
                    {
                        tracingService.Trace(Convert.ToString(seminarortId));
                        
                        teilnehmer["mrd_seminarort"] = seminarortId;
                        _serviceProxy.Update(teilnehmer);
                    }
                                       
                }


*This post is locked for comments

  • BlackBeard Profile Picture
    BlackBeard 445 on at
    RE: Update Child Records in Plugin

    Thank you Pawel :)

    It works perfectly :)

    Have a nice day!

  • Verified answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Update Child Records in Plugin

    Well currently you are not updating anything as you did not provide ID for the entity you are trying to update. Check this code with my comments:

                    //Entity teilnehmer = new Entity(); DON'T do this here, you don't have to create it here as you don't use it here
                    QueryExpression query = new QueryExpression("mrd_teilnehmer");
                    query.ColumnSet = new ColumnSet(true);
                                    
                    query.Criteria.AddCondition("mrd_ak_seminarid", ConditionOperator.Equal, seminarID);
    
                    // query ausführen
                    EntityCollection retrieveedxpan = service.RetrieveMultiple(query);
    
                    tracingService.Trace(Convert.ToString(retrieveedxpan.Entities.Count));
    
                    if (retrieveedxpan.Entities.Count == 0)
                    {
                        // Template nicht gefunden
                        tracingService.Trace("Es wurden keine Teilnehmer gefunden!");
                    }
                    else
                    {
                        
                        foreach (var entity in retrieveedxpan.Entities)
                        {
                            //now it makes sense to create teilnehmer
                            Entity teilnehmer = new Entity("mrd_teilnehmer"); //but you have to provide name of the entity!
                            teilnehmer.Id = entity.Id; //And for sure you need to provide ID, if you want to update!
                            tracingService.Trace(Convert.ToString(seminarortId));
                            
                            teilnehmer["mrd_seminarort"] = seminarortId;
                            _serviceProxy.Update(teilnehmer);
                        }
                                           
                    }


  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Update Child Records in Plugin

    Hi,

    Can you share some details about your entity and related entity and on which event you are registering your plugin.

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans