I verified and there is no workflow.
Here is my code :
public class PostAccountUpdateContacts : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Get a reference to the Organization service.
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
//create the service context
var ServiceContext = new OrganizationServiceContext(service);
string oldStreet1 = "";
string newStreet1 = "";
string oldStreet2 = "";
string newStreet2 = "";
string oldCity = "";
string newCity = "";
string oldStateorProvince = "";
string newStateorProvince = "";
string oldZip = "";
string newZip = "";
string oldCountry = "";
string newCountry = "";
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
Entity preImageEntity = (Entity)context.PreEntityImages["preImageAlias"];
Entity postImageEntity = (Entity)context.PostEntityImages["postImageAlias"];
if (preImageEntity.Attributes.Contains("address1_line1"))
{
oldStreet1 = (string)preImageEntity.Attributes["address1_line1"];
}
if (postImageEntity.Attributes.Contains("address1_line1"))
{
newStreet1 = (string)postImageEntity.Attributes["address1_line1"];
}
if (preImageEntity.Attributes.Contains("address1_line2"))
{
oldStreet2 = (string)preImageEntity.Attributes["address1_line2"];
}
if (postImageEntity.Attributes.Contains("address1_line2"))
{
newStreet2 = (string)postImageEntity.Attributes["address1_line2"];
}
if (preImageEntity.Attributes.Contains("address1_city"))
{
oldCity = (string)preImageEntity.Attributes["address1_city"];
}
if (postImageEntity.Attributes.Contains("address1_city"))
{
newCity = (string)postImageEntity.Attributes["address1_city"];
}
if (preImageEntity.Attributes.Contains("address1_stateorprovince"))
{
oldStateorProvince = (string)preImageEntity.Attributes["address1_stateorprovince"];
}
if (postImageEntity.Attributes.Contains("address1_stateorprovince"))
{
newStateorProvince = (string)postImageEntity.Attributes["address1_stateorprovince"];
}
if (preImageEntity.Attributes.Contains("address1_postalcode"))
{
oldZip = (string)preImageEntity.Attributes["address1_postalcode"];
}
if (postImageEntity.Attributes.Contains("address1_postalcode"))
{
newZip = (string)postImageEntity.Attributes["address1_postalcode"];
}
if (preImageEntity.Attributes.Contains("address1_country"))
{
oldCountry = (string)preImageEntity.Attributes["address1_country"];
}
if (postImageEntity.Attributes.Contains("address1_country"))
{
newCountry = (string)postImageEntity.Attributes["address1_country"];
}
if (oldStreet1 != newStreet1 || oldStreet2 != newStreet2 || oldCity != newCity || oldZip != newZip || oldStateorProvince != newStateorProvince || oldCountry != newCountry)
{
try
{
//Create query to get the related contacts
var res = from c in ServiceContext.CreateQuery("contact")
where c["ch_direct"].Equals(entity.Id)
select c;
foreach (var c in res)
{
Entity e = (Entity)c;
if (oldStreet1 != newStreet1)
e["address1_line1"] = newStreet1;
if (oldStreet2 != newStreet2)
e["address1_line2"] = newStreet2;
if (oldCity != newCity)
e["address1_city"] = newCity;
if (oldZip != newZip)
e["address1_postalcode"] = newZip;
if (oldStateorProvince != newStateorProvince)
e["address1_stateorprovince"] = newStateorProvince;
if (oldCountry != newCountry)
e["address1_country"] = newCountry;
//ServiceContext.Attach(e);
ServiceContext.UpdateObject(e);
}
ServiceContext.SaveChanges();
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
}