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