CRM 2011 – getting the state of a record and using setStateDynamicEntity

This week I was trying to trigger a workflow when a User record was disabled/enabled.

I haven’t written a workflow triggered by a change of state before and it is a little bit unusual.

Like many organisations we have Users and then replicate the users in a company and contacts.  I was hoping to trigger actions with a workflow but curiously a lot of the triggers for the User (SystemUser) are disabled for reasons I can’t really work out.

So what I needed to do was to have a plugin which is fired when the status of the SystemUser is changed.

When I was creating the plugin you have a choice of SetState or SetStateDynamicEntity, I initially tried the setState because I was assuming the setStateDynamicEntity would be needed for custom entities but the SetState never got triggered.

I created a plugin for the SetStateDynamicEntity which got triggered.

This blog seems to have had the same problem although in CRM 4

Anyway the next curious thing is on a setState plugin the usual Entity object is not passed in and instead you get “EntityMoniker” which holds an EntityReference, State and Status.

The above topic reminds me of an answer I gave to a linkedin discussion, where someone was saying they just hire good C# developers rather than CRM developers (who are harder to come by).  The above knowledge shows the experience you build up as a CRM developer which wouldn’t be known by a C# developer.  The C# code written for CRM is quite straight forward and not very complex but understanding all the CRM coding quirks and how the whole CRM development infrastructure works takes a lot of trail and error.

Even to start with you have to learn about the PluginRegistration tool, although now this has been replaced with the Visual studio tool but to get the Organisation connection takes time.

So basically what I am saying is CRM developers are worth there weight in gold and should be paid loads, especially me.

Back to the blog at hand.  Below is the code to check for the EntityMoniker and then to get the EntityReference and the two optionSets which contains the State and Status.

if (context.InputParameters.Contains("EntityMoniker") &&
 context.InputParameters["EntityMoniker"] is EntityReference)
 EntityReference myEntity = (EntityReference)context.InputParameters["EntityMoniker"];
 OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
 OptionSetValue status = (OptionSetValue)context.InputParameters["Status"];

//state 1 = disabling the record
 //state 0 - enabling the record

UserCode pluginCode = new UserCode(service);

pluginCode.userStatusChange(myEntity, state);


So once I worked out the status I then need to some code to enable or disable a contact record, this is also is different from standard CRM Service calls because you can’t use the Service.Update();

To update the status of a record you need to do a SetStateRequest

EntityReference contactRef = user.meta_MetaphorixContact;

SetStateRequest setState = new SetStateRequest();

setState.EntityMoniker = new EntityReference(Contact.EntityLogicalName, user.new_userContact.Id);
 setState.State = new OptionSetValue(1);
 setState.Status = new OptionSetValue(-1);
 SetStateResponse resp = (SetStateResponse)service.Execute(setState);

It’s 1 to disable and 0 to enable.  The code above disables the contact in the entityReference i passed in.

2 thoughts on “CRM 2011 – getting the state of a record and using setStateDynamicEntity

  1. Johan February 15, 2013 / 5:39 am

    “trail and error” <– should be "trial and error"
    "CRM developers are worth there weight in gold" <– should be "their"


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s