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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Plugin to update all the related contacts of specific account when specific field on account record has been updated

(0) ShareShare
ReportReport
Posted on by 25

: Following are the details of what needs to be covered:

  1. Make sure we have Account to Contact (1:N) relationship setup calling the subgrid as Related Contacts
  2. Create a new field in Account and Contact both called as “Payment Type” which is an optionset field having options as {Cash, Debit Card, Credit Card}
  3. Create a plugin on Account entity which will be registered on “Update” of “Payment Type” field.
  4. Plugin logic will include following steps:
    1. Retrieve all related Contacts corresponding to Account in context
    2. Update “Payment Type” field on all the Contacts same as what has been set on updated Account record
I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    Use below code to update related contacts on update of payment type field on account.

    Make sure you change the code based on your need. only fetchxml/query exp and update record part needs to be modified.

    using System;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    namespace Training.Plugins

    {

       public class UpdateRelatedTrainingContact : IPlugin

       {

           public EntityCollection RetrieveContacts(IOrganizationService service, Guid companyId, int page, string pagingCookie)

           {

               if (pagingCookie != null && pagingCookie != "")

                   pagingCookie = pagingCookie.Replace("\"", "'").Replace(">", ">").Replace("<", "<");

               string fetchXml = @"<fetch version=""1.0""

               count=""5""

               page=""{1}""

               paging-cookie=""{2}""

               returntotalrecordcount=""true""

               output-format=""xml-platform""

               mapping=""logical""

               distinct=""false"">

                   <entity name=""ita_trainingcontact"">

                   <attribute name=""ita_trainingcontactid"" />

                  <filter type=""and"">

                   <condition attribute=""ita_company"" operator=""eq"" value=""{0}"" />

                   </filter>

                   </entity>

                   </fetch>";

               fetchXml = string.Format(fetchXml, companyId, page, pagingCookie);

               var qe = new FetchExpression(fetchXml);

               var result = service.RetrieveMultiple(qe);

               return result;

           }

           public EntityCollection RetrieveContactsUsingQueryExp(IOrganizationService service, Guid companyId, int page, string pagingCookie)

           {

               QueryExpression qe = new QueryExpression("ita_trainingcontact");

               qe.Criteria.AddCondition(new ConditionExpression("ita_company", ConditionOperator.Equal, companyId));

               qe.PageInfo.Count = 5;

               qe.PageInfo.PageNumber = page;

               qe.PageInfo.PagingCookie = pagingCookie;

               qe.PageInfo.ReturnTotalRecordCount = true;

               var result = service.RetrieveMultiple(qe);

               return result;

           }

           public void Execute(IServiceProvider serviceProvider)

           {

               IPluginExecutionContext context = (IPluginExecutionContext)

               serviceProvider.GetService(typeof(IPluginExecutionContext));

               ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               Entity entity = (Entity)context.InputParameters["Target"];

               if (entity.Contains("ita_priority"))

               {

                   IOrganizationServiceFactory serviceFactory =

                  (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                   int pageNumber = 1;

                   string pagingCookie = "";

                   EntityCollection result = null;

                   do

                   {

                       result = RetrieveContacts(service, entity.Id, pageNumber, pagingCookie);

                       trace.Trace("Total Contacts:" + result.TotalRecordCount.ToString());

                       foreach (var e in result.Entities)

                       {

                           Entity updatedContact = new Entity(e.LogicalName);

                           updatedContact.Id = e.Id;

                           updatedContact["ita_priority"] = entity["ita_priority"];

                           service.Update(updatedContact);

                       }

                       pagingCookie = result.PagingCookie;

                       pageNumber++;

                   } while (result.MoreRecords);

               }

           }

       }

    }

    Good luck!

    Please mark my answer verified if i were helpful

  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Hi, 

    1- there is a standard relationship between an account and a contact: Account.accountId --> Contact.parentCutsomerId

    2- You can create this field on both entities: https://docs.microsoft.com/en-us/powerapps/maker/data-platform/create-edit-fields

    3- Refer to this documentation to create your plugin: https://docs.microsoft.com/en-us/powerapps/developer/data-platform/write-plug-in

    4- Refer to this blog: https://crmknowhow.wordpress.com/2018/08/21/update-child-record-when-parent-record-is-updated-using-plugin/

    Good Luck !

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Nagaraju_Matta Profile Picture

Nagaraju_Matta 123

#2
Hamza H Profile Picture

Hamza H 112 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans