C# · Dynamics CRM 2011

CRM 2011 – Using SetStateRequest to activate or deactivate

With XRM’s early bound entity, we can do a lot of things, including quite complicated LINQ Queries. However, we cannot activate or deactivate records by simply setting statecode and statuscode in the early bound entity records. To do this, we must use SetStateRequest and SetStateResponse from Microsoft.Crm.Sdk.Proxy assembly.

IOrganizationService service;
new_entity record; // a record of new_entity

SetStateRequest setStateReq = new SetStateRequest();
setStateReq.EntityMoniker = new EntityReference(record.LogicalName, record.Id);
setStateReq.State = new OptionSetValue((int)new_entityState.InActive);
setStateReq.Status = new OptionSetValue(-1);

SetStateResponse response = (SetStateResponse)service.Execute(setStateReq);

Cheers – Sy

Advertisements

6 thoughts on “CRM 2011 – Using SetStateRequest to activate or deactivate

    1. Hi magnamus,

      Glad you worked it out. If you use Early Bound Code, you can write extension code to generate option set that include status reasons. That makes life so much easier.

      Cheers,
      Sy

      1. Actually you can, but seems these attributes (statuscode, statecode) are ignored when updating the record. Even if you do this with Entity.Attributes.

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 )

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