Hi David,
yes that is on the preMessage. Below is the full code
public class UpdateContactProfessionalGroup : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
switch (context.MessageName)
{
case "Create":
{
// create target entity and post image entity
Entity createEntity = (Entity)context.InputParameters["Target"];
Entity postMeImage = (Entity)context.PostEntityImages["postImage"];
// create string to store the post image and then add it to the codec_professionalgroups field
string postGroup = string.Empty;
postGroup = ((EntityReference)postMeImage.Attributes["codec_primaryprofessionalgroup"]).Name;
createEntity["codec_professionalgroups"] = postGroup;
if (context.Depth <= 1)
{
service.Update(createEntity);
}
break;
}
case "Update":
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity work = (Entity)context.InputParameters["Target"];
Entity postMessageImage = (Entity)context.PostEntityImages["Image"];
Entity preMessageImage = (Entity)context.PreEntityImages["Image"];
string posttopic, pretopic = string.Empty;
string oldProGroups = "";
oldProGroups = (String)preMessageImage["codec_professionalgroups"];
if (postMessageImage.Contains("codec_primaryprofessionalgroup"))
{
posttopic = ((EntityReference)postMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;
if (preMessageImage.Contains("codec_primaryprofessionalgroup"))
{
pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;
string newGroup = oldProGroups.Replace(pretopic, "");
string Progroup = newGroup + posttopic;
work["codec_professionalgroups"] = Progroup;
if (context.Depth <= 1)
{
service.Update(work);
}
}
else {
string Progroups = oldProGroups + posttopic;
work["codec_professionalgroups"] = Progroups;
if (context.Depth <= 1)
{
service.Update(work);
}
}
}
else if (!postMessageImage.Contains("codec_primaryprofessionalgroup"))
{
pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;
string newGroup = oldProGroups.Replace(pretopic, "");
work["codec_professionalgroups"] = newGroup ;
if (context.Depth <= 1)
{
service.Update(work);
}
}
else
{
posttopic = ((EntityReference)postMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;
pretopic = ((EntityReference)preMessageImage.Attributes["codec_primaryprofessionalgroup"]).Name;
string newGroups = oldProGroups + posttopic;
work["codec_professionalgroups"] = newGroups;
}
}
break;
}
default:
break;
}
}
}