Friday, 24 May 2013

Deactivate form using Entity Moniker in mscrm 2011

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″ ).