CRM 2011 – Setting a state and status of an entity using a plugin

I had created a charge entity.  I was creating charge entities of certain values when certain things had happened on regarding cases.  When a case was created it created a charge entity linked to the case, account and product etc for a certain value.

Then when other status were reached a workflow would fire off and create more charge entity’s.  Then at a certain point the user wanted to create an invoice for all the charge entities which had not been assigned to an invoice.

So I created a plugin which when a new invoice is created it goes off and searches for all the charge entities which are not been associated with an invoice.  It then assigns the newly created invoice, adds a date and then I wanted to make the charge entity inactive.  I created a new inactive state called Invoiced.  The reason I wanted to do this was because I didn’t want the charge entity being associated with any other invoices and you can’t change inactive records.

updating the charge entity was no problem, I did this by doing a service.update(entity) but I got errors if I tried to change the state of the entity.  I recalled the state being some what different.

To change the state and status you have to a setStateRequest, it took me a while to work this out.

Another unusualish thing is the state of active is 0 and inactive is 1.  I’m not sure that’s unusual or not.

The status is the status value you want, I had to look up my new status and then assign the value.  The reason you use numbers is because the state and status are optionSet which are set using numbers.  My new status had the value of 951850001 and the default of inactive I think has the value of -1.

Here is the code

public void updateChargeState(Guid chargeId)
{

SetStateRequest setState = new SetStateRequest();

setState.EntityMoniker = new EntityReference();

setState.EntityMoniker.Id = chargeId;

setState.EntityMoniker.Name = "Charge";

setState.EntityMoniker.LogicalName = new_charge.EntityLogicalName;

setState.State = new OptionSetValue(1);
setState.Status = new OptionSetValue(951850001);
SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);
}

6 thoughts on “CRM 2011 – Setting a state and status of an entity using a plugin

  1. Fergus March 23, 2012 / 10:28 am

    Hi Ben,

    Another great post! To get around the magic number issue for state and status codes we use the extensions from the Microsoft.Crm.Sdk.Samples in the SDK to generate enums for all state and status codes for entities. We just build the source provided in sdk\samplecode\cs\crmsvcutilextensions and then call crmsvcutil as follows:

    crmsvcutil.exe /codewriterfilter:”Microsoft.Crm.Sdk.Samples.FilteringService, GeneratePicklistEnums”
    /codecustomization:”Microsoft.Crm.Sdk.Samples.CodeCustomizationService, GeneratePicklistEnums”
    /namingservice:”Microsoft.Crm.Sdk.Samples.NamingService, GeneratePicklistEnums
    /url:%ORGANIZATION_SERVICE_URL%
    /out:OptionSets.cs
    /namespace:%NAMESPACE%

    The generated enums example:

    [System.Runtime.Serialization.DataContractAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute(“CrmSvcUtil”, “5.0.9689.1985”)]
    public enum account_statuscode
    {

    [System.Runtime.Serialization.EnumMemberAttribute()]
    Active = 1,

    [System.Runtime.Serialization.EnumMemberAttribute()]
    Inactive = 2,
    }

    [System.Runtime.Serialization.DataContractAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute(“CrmSvcUtil”, “5.0.9689.1985”)]
    public enum ContractDetailContractStateCode
    {

    [System.Runtime.Serialization.EnumMemberAttribute()]
    DefaultValue = 1,
    }

    Just makes it much simpler to code against the SetStateRequest.

    Cheers
    Fergus

    Like

  2. Tarek April 30, 2012 / 1:10 pm

    hi, i have a question for you whats the different between state and status?
    am new in CRM btw. Thanks

    Like

    • Hosk April 30, 2012 / 2:14 pm

      The state controls if an entity is active or inactive

      status is the current status it has. So the active state can have many status associated with it. if you think about quotes an active quote can have a status of draft, active etc

      Like

  3. roxanna November 5, 2014 / 5:12 pm

    HI Ben,
    Nice article. What if we want to change the attribute value along with the state changes.

    For example,

    foreach (var item in ec.Entities)
    {

    SetStateRequest setStateRequest = new SetStateRequest();
    setStateRequest.EntityMoniker = new EntityReference(“task”, item.Id);
    setStateRequest.State = new OptionSetValue(2);
    setStateRequest.Status = new OptionSetValue(6);

    requestWithResults.Requests.Add(setStateRequest);
    }

    Now if I want to set an attribute value of ‘task’ entity to somevalue along with the above code.
    How can this be accomplished?
    Thanks

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.