How to write plugin to Update Child records when Parent record updated (lookup filed and option set )
*This post is locked for comments
*This post is locked for comments
.. You can use Flow without Coding. Fetch the Childs, loop and Update.
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)
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); } } } }
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
André Arnaud de Cal... 291,391 Super User 2024 Season 2
Martin Dráb 230,445 Most Valuable Professional
nmaenpaa 101,156