0
votes

I'm working on a CRM 2011 plugin that changes one field's value on an account entity if user deactivates the account. I spent a lot of time wondering what's wrong because I received the following error every time I deactivated some account

"Error. An error has occured. Try this action again. If the problem continues, check the Microsoft Dynamics CRM Community for solutions or contact your organization's Microsoft Dynamics CRM Administrator. Finally, you can contact Microsoft Support"

But after some time I noticed that even if the error my plugin actually works perfectly. My code is below just in case (notice that we call our accounts as clients)

Entity client = (Entity)context.InputParameters["Target"];

OptionSetValue state = (OptionSetValue)client["statecode"];

if (state.Value == 1)
{
    OptionSetValue clientStatus = new OptionSetValue(100000000);
    client["customertypecode"] = clientStatus;                   
    service.Update(client);
}

So does anyone has any thoughts what could cause this problem? If I disable my plugin and then deactivate any account it works perfectly without any errors.

My plugin is registered at Pre-operation stage synchronously.

Thank you in advance!

2
Can't see anything wrong with your code. Can you put whole plugin code? Even its worth trying to register the plugin again.Scorpion

2 Answers

0
votes

When your plugin is subscribed to the SetState or SetStateDynamicEntity message, the entity is not in the IPluginExecutionContext.InputParameters["Target"].
These messages have three InputParameters:

  • "EntityMoniker" (EntityReference)
  • "State" (OptionSetValue)
  • "Status" (OptionSetValue)

So there is no "Target".

EntityReference clientRef = context.InputParameters["EntityMoniker"] as EntityReference;
OptionSetValue newStateCode = context.InputParameters["State"] as OptionSetValue;

if (newStateCode.Value == 1)
{
    Entity updateClient = new Entity(clientRef.LogicalName);
    updateClient.Id = clientRef.Id;
    updateClient["customertypecode"] = new OptionSetValue(100000000);

    service.Update(updateClient);
}

When your plugin is subscribed to the Update message:

Since you're in the Pre-operation stage and the target Entity is the actual Entity you want to update, why do you call service.Update? Just add the property to the target Entity and be done with it...

Entity client = (Entity)context.InputParameters["Target"];

OptionSetValue state = (OptionSetValue)client["statecode"];

if (state.Value == 1)
{
    OptionSetValue clientStatus = new OptionSetValue(100000000);
    client["customertypecode"] = clientStatus;
}
0
votes

So your plugin is registered on the Pre-Operation of the SetStateDynamic message? And all you're attempting to do is update the customertypecode? My guess, since you haven't shown your code, is that you're not getting the IOrganizationService from the plugin context.