Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to write plugin to Update Child records when Parent record updated

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

How to write plugin to Update Child records when Parent record updated (lookup filed and option set )

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to write plugin to Update Child records when Parent record updated

    .. You can use Flow without Coding.  Fetch the Childs, loop and Update.

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: How to write plugin to Update Child records when Parent record updated

    You have to follow below steps:

    1.Register Plugin on message update and Entity will be parent record Entity.

    2. Select fields in filter Attribute on which you want this plugin get execute.

    3. In code, You have to fetch all children records of parent. (You will get parent record id from plugin Target)

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to write plugin to Update Child records when Parent record updated

    Hi ,

    You need to get the entity id of the parent record in the plugin to retrieve the child records (use retrievemultiple  message and pass fetchXML  passing entity id ). Once you get the child record  loop through the entity collection and using organization service update the child record.

    public void Execute(IServiceProvider serviceProvider)
    
           {
    
               if (serviceProvider == null)
    
                   throw new ArgumentNullException("serviceProvider");
    
               var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
               var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
               var service = serviceFactory.CreateOrganizationService(context.UserId);            
    
               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    
               {
    
                    Entity targetAccount = (Entity)context.InputParameters["Target"];
    
                   if (targetAccount.Contains("telephone1"))
    
                   {
    
                       // Retrieve all contact for this account
    
                       var fetch = @"<fetch no-lock='true' >
    
                                 <entity name='contact' >
    
                                   <attribute name='contactid'/>
    
                                   <filter>
    
                                     <condition attribute='parentcustomerid' operator='eq' value='{0}' />
    
                                   </filter>
    
                                 </entity>
    
                               </fetch>";
    
                       var fetchXML = string.Format(fetch, targetAccount.Id);
    
                       var allContacts = service.RetrieveMultiple(new FetchExpression(fetchXML)).Entities;                  
    
                       // Iterate through all contact record and update the phone number from account
    
                       foreach (var contactEnt in allContacts)
    
                       {
    
                           Entity contactToUpdate = new Entity(contactEnt.LogicalName, contactEnt.Id);
    
                           contactToUpdate["telephone1"] = targetAccount["telephone1"];
    
                           service.Update(contactToUpdate);
    
                       }
    
                   }
    
               }
    
           }
  • Abhishek Gupta Profile Picture
    Abhishek Gupta 2,001 on at
    RE: How to write plugin to Update Child records when Parent record updated

    HI,

    Write a code activity to fetch all the child records using query expression. Once you get all child records iterates through collection to update child record. Finally trigger your plugin on update of parent record. Make use of filtering attributes on Parent record name attribute or Option set attribute.

    Thank you

    Abhishek

    If found useful, please mark the answer as verified

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans