Hi,
I have created a plugin such that when the address fields in account entity are updated, i have update the corresponding contact addresses also. I registered in Update account in postoperation.
entityContact["address1_city"] = accountEntity.Attributes.Contains("address1_city") ? accountEntity["address1_city"].ToString() : string.Empty;
We can get the fields in the context if only we update them right ? If we are not updating the city field in account entity, then the corresponding city field in contact entity should assign the empty value as per the above code... But, eventhough i haven't updated the fields in account, the existing values are retained in the contact. Can you please explain how it happens ?
*This post is locked for comments
Hi Ravi,
I have not did anything on filtering attributes... by default it was all selected.
This is my code...
public void Execute(IServiceProvider _serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)_serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)_serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity accountEntity = (Entity)context.InputParameters["Target"];
QueryExpression query = new QueryExpression();
query.EntityName = "contact";
query.ColumnSet.AllColumns = true;
query.Criteria.Conditions.Add(new ConditionExpression("parentcustomerid", ConditionOperator.Equal, accountEntity.Id));
EntityCollection entityCollection = _service.RetrieveMultiple(query);
foreach (Entity entityContact in entityCollection.Entities)
{
if (entityContact.LogicalName == "contact")
{
entityContact["address1_city"] = accountEntity.Attributes.Contains("address1_city") ? accountEntity["address1_city"].ToString() : string.Empty;
entityContact["address1_line1"] = accountEntity.Attributes.Contains("address1_line1") ? accountEntity["address1_line1"].ToString() : string.Empty;
entityContact["address1_line2"] = accountEntity.Attributes.Contains("address1_line2") ? accountEntity["address1_line2"].ToString() : string.Empty;
entityContact["address1_line3"] = accountEntity.Attributes.Contains("address1_line3") ? accountEntity["address1_line3"].ToString() : string.Empty;
entityContact["address1_country"] = accountEntity.Attributes.Contains("address1_country") ? accountEntity["address1_country"].ToString() : string.Empty;
entityContact["address1_stateorprovince"] = accountEntity.Attributes.Contains("address1_stateorprovince") ? accountEntity["address1_stateorprovince"].ToString() : string.Empty;
_service.Update(entityContact);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Hi Anand,
Your code seems fine. Please check what is your trigger point of plugin? As suggested by Ravi also, If you want to map Account's Address to all Contact's Address, then Plugin should be triggered on all account address field's update. (Select all address field under Filtered Attribute dropdown list in Plugin Registration Tool).
Hope it helps
If my answer helped to resolve your issue, kindly verify it by clicking 'Yes'. It would be helpful to the other community members seeking to resolve a similar issue.
Cheers
Arpit
Hi,
Yes it should as suggested by Ravi, You share your comple updated code here, with registration details for checking
Hi,
Yes, if the fields are not updated, it won't be appear in the plugin's Target entity
1) Could you please check if you have set any attribute filtering to execute this plugin. If yes then the plugin won't trigger unless you change the field values mentioned in the attribute filter.
2) Also, accountEntity is retrieved from the plugin target property?
Entity target = (Entity)context.InputParameters["Target"];
If accountEntity is an image then it will contains those field if they have value in it.
Hope this helps.
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,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156