Skip to main content

Notifications

Announcements

No record found.

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

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
  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Plugin to update all the related contacts of specific account when specific field on account record has been updated

    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 !

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Plugin to update all the related contacts of specific account when specific field on account record has been updated

    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

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