, , ,

Hello Everyone,

I was thinking to write this since quite a long time, as being a Technical Consultant I always need these pieces of codes handy. Just like me many of you might be struggling to change the State and Status of an Entity record.

In the beginning times I tried to update the entity with Status and State value. But it always resulted in Error. Then after some reading I realized that there is a SetStateRequest Class which we use to do the same.

In my recent development; I was trying to Reactivate a Disqualified lead. So I used the following code below to achieve the result.

// LeadId contains the Id for the Lead to be Reactivated

SetStateRequest setStateRequest = new SetStateRequest() {
EntityMoniker = new EntityReference{Id = leadId, LogicalName = "lead"},
State = new OptionSetValue(0),
Status = new OptionSetValue(1)

Note: The default State and Status code for Lead are following:

(StatusCode may differ on the value of the Optionset under StatusReason)

                     StateCode                       StatusCode
Open                    0                               1,2
Qualify                 1                                3
Disqualify              2                             4,5,6,7

Similarly, we can use SetStateRequest to Activate and Deactivate any entity record by changing their StateCode and StatusCode. You can find the list of State and Status Codes here.

Hope that was Helpful.

Thanks !!!