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

Posted on by 95

Hi everyone

How to write plugin on Account entity when Account Field Updated and i need to update all child records associated with that account.

*This post is locked for comments

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

    This is accepted answer. Thanks Ravi.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How to write plugin to Update Child records when Parent record updated

    Hi Sreeekanth,

    You can refer the below code, this update the Phone [telephone1] of account entity to all its related contacts.

    -------------------

    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);

                       }

                   }

               }

           }

    -----------------------------------------------

    Hope this helps.

  • Sreeekanth Profile Picture
    Sreeekanth 95 on at
    RE: How to write plugin to Update Child records when Parent record updated

    hiiii ALEX

                 the code what they written inside  the function

    protected void ExecutePostAccountUpdateContacts(LocalPluginContext localContext)

    i am not getting.

    Basically we use Base class Iplugin to write plugin and

    we will override Execute Method to get required data .

    So can you help me regarding code 

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: How to write plugin to Update Child records when Parent record updated

    Hi Sreekanth,

    If you are using early bound and lets say you have Contact is child record for this Account, then you should retrieve contacts like this.

    using (var xrm = new ServiceContext(service))              // ServiceContext is your XRM file Context Name
    {
    var contactRecords = xrm.ContactSet.Where(c => c.ParentCustomerId.Id == account.Id).ToList();  

    // You should compare Contact Entity ACcount Lookup field GUID with current Account Id. Here ParentCustomerId is account Lookup
    foreach (var contactRecord in contactRecords)
    {

    // Update each Child Contact Entity 
    service.Update(new Contact
    {
    Id = contactRecord.Id,
    LastName = "Record Last Name"
    });
    }

    }

  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    Rajkumar Rajaraman 18,108 on at
    RE: How to write plugin to Update Child records when Parent record updated

    Refer this:

    community.dynamics.com/.../how-to-update-the-parent-record-when-child-record-is-created-or-updated

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to write plugin to Update Child records when Parent record updated

    Hi,

     here is an example for contacts(or, at least, one way to do it):

    mscrmshop.blogspot.ca/.../plugin-to-update-children-records-when.html

     you will need to do the same for all other child entities you want to update

  • Sreeekanth Profile Picture
    Sreeekanth 95 on at
    RE: How to write plugin to Update Child records when Parent record updated

    Hi Priyesh

    Could please tell me How to retrieve all child records that are associated with account

  • Suggested answer
    Priyesh Profile Picture
    Priyesh 7,392 User Group Leader on at
    RE: How to write plugin to Update Child records when Parent record updated

    Here are the high level steps -

    1. Write a plugin on the update of the Account i.e. on the field which you want to trigger this plugin.

    2. In the plugin, retrieve the child records where the Account lookup in them is the current record (Account record) from the Context.

    3. Then, run a loop on the retrieved Active Records to update data you want to update in those child records.

    End.

    Hope this is helpful.

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

Featured topics

Product updates

Dynamics 365 release plans