Michael Philip's Blog

Writing about MS Tech with strong focus on Azure & CRM

Using SetStateRequest to deactivate a record- MS CRM 2011

Leave a comment

IOrganizationService service;
new_entity record;  // a record of new_entity, can be any Entity.
SetStateRequest setStateReq = new SetStateRequest();
setStateReq.EntityMoniker = new EntityReference(record.LogicalName, record.Id);

//State represents the field with schema name “state code” and display name “status”.
//Suppose we are working on the account entity  in which the state code field has to be 1 so that the record be inactive.
setStateReq.State = new OptionSetValue(1);

//Status  represents the field with schema name “Status code” and display name “Status Reason”.
// set this value to -1 to let the system automatically set the appropriate corresponding status value.
setStateRequest.Status = new OptionSetValue(-1);

// Execute the request
SetStateResponse response = (SetStateResponse)service.Execute(setStateReq);

Note:  you can know at which values your entity is active or inactive by going to customizations , opening your entity then opening  field “status code” and checking the items of the drop down new the label “status” which are the values of the state code variable, you will see the text only and you can know the value as the value of the first item is 0 and every item after it  its value is incremented by 1(if we took the screenshot as an example the value of “Active” will be “0” and the value of Inactive will be “1” ).



Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s