Thursday, 16 April 2015

Set Status(Activate/De-activate) of the CRM record

Hi,

We do activate and deactivate CRM record more often. Sometimes, we get the requirement to do the same in the plugin/workflow/some job.

Here is the code for activating and de-activating the CRM record.


private void SetStatus(bool active, string entityName, Guid entityId, IOrganizationService objService)
{
       SetStateRequest objSetStateRequest = new SetStateRequest();
       objSetStateRequest.EntityMoniker = new EntityReference(entityName, entityId);
       if (!active)
       {
             objSetStateRequest.State = new OptionSetValue(1);
             objSetStateRequest.Status = new OptionSetValue(2);
       }
       else
       {
             objSetStateRequest.State = new OptionSetValue(0);
             objSetStateRequest.Status = new OptionSetValue(1);
       }
       SetStateResponse objSetStateResponse = (SetStateResponse)objService.Execute(objSetStateRequest);
}

Hope this helps

--
Happy CRM'ing
Gopinath

No comments:

Post a Comment