web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

*This post is locked for comments

I have the same question (0)
  • Abhishek Gupta Profile Picture
    2,003 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

  • Verified answer
    gdas Profile Picture
    50,091 Moderator 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);
    
                       }
    
                   }
    
               }
    
           }
  • Suggested answer
    Dynamics365 Rocker Profile Picture
    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)

  • Community Member Profile Picture
    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans