Thursday, 14 April 2016

How to retrieve SLA Pause States (On Hold Case Status) in C# code


Following code shows how to retrieve SLA Pause States from Dynamics CRM using C#. I needed code a functionality to calculate case elapsed minutes and I thought it is worth finding a way to load paused statuses from system rather than hard-coding them.


        private static string[] GetSlaPauseStates(IOrganizationService service)
        {
            QueryExpression qe = new QueryExpression("organization")
            {
ColumnSet = new ColumnSet(new string[] { "organizationid", "slapausestates" })
            };

            EntityCollection orgs = service.RetrieveMultiple(qe);

            if (orgs != null && orgs.Entities.Count > 0)
            {
                var org = orgs[0];
                var slaPauseStates = orgs[0]["slapausestates"];
                string[] states = slaPauseStates.ToString().Split(';');
                return states;
            }

            return null;
        }

In Plugin or Custom Workflow we can retrieve the OrganizationId from contact and then we can directly retrieve the Organization by Id. Following is the code to retrieve organizationId.


    IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    IOrganizationServiceFactory serviceFactory =      executionContext.GetExtension<IOrganizationServiceFactory>();
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    var organizationId = context.OrganizationId;

    // Get Organization Business Closure Calendar Id
    var organization = service.Retrieve("organization", context.OrganizationId, new ColumnSet("slapausestates"));
    var slaPauseStates = organization["slapausestates"];

    string[] states = slaPauseStates.ToString().Split(';');


Happy Coding

P. S. Hayer
(ਪ੍ਰੇਮਜੀਤ ਸਿੰਘ ਹੇਰ)