Programmatically Activate and Deactivate SLA Records in Dynamics CRM

By | April 29, 2016

Introduction:

Service Level Agreements (SLAs) in Microsoft Dynamics CRM help you define the level of support or service that the company agrees to offer to its client. The details items that are included to define key performance indicators (KPIs) or metrics to achieve the service level that is expected to be.

With the release of Microsoft Dynamics CRM 2016, ‘UpdateRequest’ was introduced to activate and deactivate SLA records programmatically.

When we import the cases the related SLA records have to be deactivated programmatically so that it is not used along with the cases. We can now do this using ‘UpdateRequest’ for activating or deactivating the SLA records. This saves the time and the effort of deactivating the SLA records manually.

Let’s take a look how the ‘UpdateRequest’ is used in C# and in the code block.

Using ‘UpdateRequest’ through C#

//Get Case Guid
            Guid entitlementId = Guid.Empty;
            Guid slaId = Guid.Empty;
            Guid CaseId = new Guid("17145E3A-A7D7-E511-80DC-FC15B4284AE0");

            //Retrieve Case to get entitlement
            Entity caseRes = _orgService.Retrieve("incident", CaseId, new ColumnSet(new string[] { "title", "entitlementid" }));

            if (caseRes != null && caseRes.Contains("entitlementid"))
            {
                //Get entitlement Id
                entitlementId = ((EntityReference)caseRes.Attributes["entitlementid"]).Id;
            }

            //Retrieve Entitlement to get SLA
            Entity entitleEntity = _orgService.Retrieve("entitlement", entitlementId, new ColumnSet(new string[] { "slaid" }));

            if (entitleEntity != null && entitleEntity.Contains("slaid"))
            {
                //Get SLA Id
                slaId = ((EntityReference)entitleEntity.Attributes["slaid"]).Id;
            }

            //Deactivate SLA Record
            Entity slaEntity = new Entity("sla");
            slaEntity.Id = slaId;
            slaEntity["statecode"] = new OptionSetValue(0);
            slaEntity["statuscode"] = new OptionSetValue(1);

            _orgService.Update(slaEntity);

Using ‘UpdateRequest’ through scripting

function DeactivateSLA() {
    try {
        var slaId = null;
        var caseId = Xrm.Page.data.entity.getId();
        var entitlement = Xrm.Page.getAttribute("entitlementid").getValue();
        var entitlementId = entitlement[0].id;
        var cols = ["slaid"];

        //Retrieve Entitlement record
        var entitleResponse = XrmServiceToolkit.Soap.Retrieve("entitlement", entitlementId, cols, null);

        //Retrieve entitlement to get SLA
        if (entitleResponse != null && entitleResponse.attributes["slaid"] != null) 
        {
            slaId = entitleResponse.attributes["slaid"].id;
        }

        //Update SLA record
        var slaEntity = {};
        slaEntity.StateCode = { Value: 0 };
        slaEntity.StatusCode = { Value: 1 };
       
        //Update SLA Status
        XrmServiceToolkit.Rest.Update(slaId, slaEntity, "SLASet", function (result) 
        {
            Xrm.Utility.alertDialog("SLA Deactivated Successfully" + Id);
        },
          function (err) { return (err.Message) }, false);
    }
    catch (e) 
    {
        Xrm.Utility.alertDialog("Function Name:" + "DeactivateSLA" + "-" + e.description);
    }
}

Conclusion:

On a concluding note, hope the above code blocks help you to use ‘UpdateRequest’ to activate and deactivate the SLAs without affecting the cases. Any other SDK messages except ‘UpdateRequest’ are still not supported for this entity.

Now clone your Dynamics CRM Records in just 1 Click with Click2Clone.

One thought on “Programmatically Activate and Deactivate SLA Records in Dynamics CRM

Comments are closed.