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
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
This is accepted answer. Thanks Ravi.
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.
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
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"
});
}
}
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
Hi Priyesh
Could please tell me How to retrieve all child records that are associated with account
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,280 Super User 2024 Season 2
Martin Dráb 230,214 Most Valuable Professional
nmaenpaa 101,156