{"id":2689,"date":"2016-04-29T17:54:54","date_gmt":"2016-04-29T12:24:54","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=2689"},"modified":"2016-04-29T17:54:54","modified_gmt":"2016-04-29T12:24:54","slug":"programmatically-activate-and-deactivate-sla-records","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2016\/04\/programmatically-activate-and-deactivate-sla-records\/","title":{"rendered":"Programmatically Activate and Deactivate SLA Records in Dynamics CRM"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">With the release of Microsoft Dynamics CRM 2016, <strong>\u2018UpdateRequest\u2019<\/strong> was introduced to activate and deactivate SLA records programmatically.<\/p>\n<p style=\"text-align: justify;\">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 \u2018UpdateRequest\u2019 for activating or deactivating the SLA records. This saves the time and the effort of deactivating the SLA records manually.<\/p>\n<p style=\"text-align: justify;\">Let\u2019s take a look how the <strong>\u2018UpdateRequest\u2019<\/strong> is used in C# and in the code block.<\/p>\n<p style=\"text-align: justify;\"><strong>Using \u2018UpdateRequest\u2019 through C#<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\/\/Get Case Guid\n            Guid entitlementId = Guid.Empty;\n            Guid slaId = Guid.Empty;\n            Guid CaseId = new Guid(\"17145E3A-A7D7-E511-80DC-FC15B4284AE0\");\n\n            \/\/Retrieve Case to get entitlement\n            Entity caseRes = _orgService.Retrieve(\"incident\", CaseId, new ColumnSet(new string[] { \"title\", \"entitlementid\" }));\n\n            if (caseRes != null &amp;&amp; caseRes.Contains(\"entitlementid\"))\n            {\n                \/\/Get entitlement Id\n                entitlementId = ((EntityReference)caseRes.Attributes[\"entitlementid\"]).Id;\n            }\n\n            \/\/Retrieve Entitlement to get SLA\n            Entity entitleEntity = _orgService.Retrieve(\"entitlement\", entitlementId, new ColumnSet(new string[] { \"slaid\" }));\n\n            if (entitleEntity != null &amp;&amp; entitleEntity.Contains(\"slaid\"))\n            {\n                \/\/Get SLA Id\n                slaId = ((EntityReference)entitleEntity.Attributes[\"slaid\"]).Id;\n            }\n\n            \/\/Deactivate SLA Record\n            Entity slaEntity = new Entity(\"sla\");\n            slaEntity.Id = slaId;\n            slaEntity[\"statecode\"] = new OptionSetValue(0);\n            slaEntity[\"statuscode\"] = new OptionSetValue(1);\n\n            _orgService.Update(slaEntity);<\/pre>\n<p><strong>Using \u2018UpdateRequest\u2019 through scripting <\/strong><\/p>\n<pre class=\"lang:default decode:true  \">function DeactivateSLA() {\n    try {\n        var slaId = null;\n        var caseId = Xrm.Page.data.entity.getId();\n        var entitlement = Xrm.Page.getAttribute(\"entitlementid\").getValue();\n        var entitlementId = entitlement[0].id;\n        var cols = [\"slaid\"];\n\n        \/\/Retrieve Entitlement record\n        var entitleResponse = XrmServiceToolkit.Soap.Retrieve(\"entitlement\", entitlementId, cols, null);\n\n        \/\/Retrieve entitlement to get SLA\n        if (entitleResponse != null &amp;&amp; entitleResponse.attributes[\"slaid\"] != null) \n        {\n            slaId = entitleResponse.attributes[\"slaid\"].id;\n        }\n\n        \/\/Update SLA record\n        var slaEntity = {};\n        slaEntity.StateCode = { Value: 0 };\n        slaEntity.StatusCode = { Value: 1 };\n       \n        \/\/Update SLA Status\n        XrmServiceToolkit.Rest.Update(slaId, slaEntity, \"SLASet\", function (result) \n        {\n            Xrm.Utility.alertDialog(\"SLA Deactivated Successfully\" + Id);\n        },\n          function (err) { return (err.Message) }, false);\n    }\n    catch (e) \n    {\n        Xrm.Utility.alertDialog(\"Function Name:\" + \"DeactivateSLA\" + \"-\" + e.description);\n    }\n}<\/pre>\n<p><strong>Conclusion:<\/strong><\/p>\n<p style=\"text-align: justify;\">On a concluding note, hope the above code blocks help you to use <strong>\u2018UpdateRequest\u2019<\/strong> to activate and deactivate the SLAs without affecting the cases. Any other SDK messages except <strong>\u2018UpdateRequest\u2019<\/strong> are still not supported for this entity.<\/p>\n<p style=\"text-align: justify;\">Now clone your Dynamics CRM Records in just 1 Click with <a href=\"http:\/\/bit.ly\/1SAyYIb\" target=\"_blank\" rel=\"noopener noreferrer\">Click2Clone<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2016\/04\/programmatically-activate-and-deactivate-sla-records\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15,19,24],"tags":[86,1632],"class_list":["post-2689","post","type-post","status-publish","format-standard","hentry","category-development","category-dynamics-crm","category-dynamics-crm-2016","tag-activate-and-deactivate-sla-records","tag-sla-in-dynamics-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=2689"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2689\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=2689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=2689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=2689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}