Hi Communities,
I created simple plugin : Whenever account updated that primarycontact fields also updated.
but I encountered the below business process error.
Please give me best solution.
coding:
namespace updatecontact
{
public class UpdateContact : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") & context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "account")
try
{
EntityReference contactId = (EntityReference)entity.Attributes["primarycontactid"];
Guid primaryContactGUID = contactId.Id;
Entity contact = new Entity("contact");
// Update the postal code attribute.
contact["address1_city"] = "Texas";
// The address 2 postal code was set accidentally, so set it to null.
contact["emailaddress1"] = "john334@gmail.com
contact["contactid"] = primaryContactGUID;
// Update the account.
service.Update(contact);
}
catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
*This post is locked for comments