We're on CRM 2016 On-Premise. I have registered a plugin that gets triggered when two fields are being updated: fieldA and fieldB. The plugin will encrypt both fields, but for some reason it would only update the first field: fieldA
We registered the plugin as Post-Operation. We also registered a PostImage with both fieldA and fieldB as the filter. However for some reason it would only update fieldA and not fieldB.
Here's my code. I put a 'throw new InvalidPluginExecutionException' after the assignment of fieldB, but for some reason it is never reached. If I put InvalidPluginExecutionException BEFORE the assignment of fieldB (but still inside the if condition), then I would receive the error message.
I am not sure what is wrong with the assignment...
string fieldA;
string fieldB;
var targetEntity = context.GetParameterCollection(context.InputParameters, "Target");
if (targetEntity == null)
throw new InvalidPluginExecutionException(OperationStatus.Failed, "Target Entity cannot be null");
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.Depth == 1)
{
var postImage = context.PostEntityImages["PostImage"];
if (postImage == null)
throw new InvalidPluginExecutionException(OperationStatus.Failed, "Post Image is required");
var account = context.GenerateCompositeEntity(targetEntity, postImage);
if (targetEntity.Attributes.Contains("new_fieldA"))
{
fieldA = account.Attributes["new_fieldA"].ToString();
targetEntity["new_fieldA"] = encrypt(fieldA);
}
if (targetEntity.Attributes.Contains("new_fieldB"))
{
fieldB = account.Attributes["new_fieldB"].ToString();
throw new InvalidPluginExecutionException("test222"); //for some reason this message never shows up
//targetEntity["new_fieldB"] = encrypt(fieldB);
//targetEntity["new_fieldB"] = "hello";
}
service.Update(targetEntity);