plz Correct my code ...."alrdy writen a plugin to update a Contact adres using Account name lookup" .......... if i update a address field in existing Account record it should update all related records in contact Addres fild .my cod is
public class UpdateContactAddress : IPlugin
{
public void Execute(IServiceProvider _serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)_serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)_serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
Entity entityAccount = (Entity)context.InputParameters["Target"];
// if (entityAccount["parentcustomerid"] != null)
{
// Guid parentId = ((EntityReference)entityAccount["parentcustomerid"]).Id;
QueryExpression query = new QueryExpression();
query.ColumnSet.AllColumns = true;
EntityCollection entityCollectionContact = _service.RetrieveMultiple(query);
foreach (Entity entityContact in entityCollectionContact.Entities)
{
if (entityContact.LogicalName == "contact")
{
if (entityContact["address1_city"] != null)
{
entityAccount["address1_city"] = entityContact["address1_city"].ToString();
}
if (entityContact["address1_line1"] != null)
{
entityAccount["address1_line1"] = entityContact["address1_line1"].ToString();
}
if (entityContact["address1_line2"] != null)
{
entityAccount["address1_line2"] = entityContact["address1_line2"].ToString();
}
if (entityContact["address1_line3"] != null)
{
entityAccount["address1_line3"] = entityContact["address1_line3"].ToString();
}
if (entityContact["address1_country"] != null)
{
entityAccount["address1_country"] = entityContact["address1_country"].ToString();
}
if (entityContact["address1_stateorprovince"] != null)
{
entityAccount["address1_stateorprovince"] = entityContact["address1_stateorprovince"].ToString();
}
_service.Update(entityContact);
}
}
}
}
}






