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
(ਪ੍ਰੇਮਜੀਤ ਸਿੰਘ ਹੇਰ)
Premjit Singh
Attendite Ltd.
Twitter

Wednesday 3 February 2016

How to reset Auto-numbering in Dynamics CRM Online


In past I have shown how to reset auto-numbering un-supported way. Today I am going to discuss how we can reset auto-numbering by Microsoft supported way.

Number greyed-out so it can't be changed through UI. But we can write a small console application to update it using SDK.



Following code can be used to update the number. In this example I am updating CurrentKbNumber but we can also update CurrentCaseNumber, CurrentContractNumber, CurrentQuoteNumber, CurrentOrderNumber and CurrentInvoiceNumber. However we don't need to worry about prefix and they can be updated through UI.

    // QueryExpression to Retrieve Organization
    QueryExpression qe = new QueryExpression("organization")
    {
        ColumnSet = new ColumnSet(new string[] {"organizationid", "name", "currentkbnumber"})
    };
    EntityCollection orgs = service.RetrieveMultiple(qe);

    if (orgs != null && orgs.Entities.Count > 0)
    {
        var org = orgs[0];
        var organizationId = (Guid)org["organizationid"];

        // Creating a new Object to update. Set the CurrentKbNumber to your desired number
        Organization organizationToUpdate = new Organization { CurrentKbNumber = 100000, Id = organizationId};
        service.Update(organizationToUpdate);
    }    

Once updated, please go to Settings > Administration > Auto-numbering to verify.

Happy Coding

P. S. Hayer

Premjit Singh
Attendite Ltd.
Twitter