Thursday, April 3, 2014

How to Close Incident using SOAP on CRM 2011

Hi Guys,

The IncidentResolution entity is a particular entity that we can not modify or form neither fields.

So when you need to put an automatic value in Resolution field (subject) you cannot customize form by adding jscript code.

The only way to do it, is by hiding the Resolve Case button:on Incident form/home page Ribbon, and to create your own custom resolution window or dialog and should send a SOAP request to the CRM Server to close the incident.

There is the most complete source code that you allow to close your incident by sending : Subject, TimeSpent and Description data. This code is synchronous.

Source Code
  1. if (typeof (Sdk) == "undefined")
  2. { Sdk = { __namespace: true }; }
  3. //This will establish a more unique namespace for functions in this library. This will reduce the
  4. // potential for functions to be overwritten due to a duplicate name when the library is loaded.
  5. Sdk.Tools = {
  6.     _getServerUrl: function () {
  7.         ///<summary>
  8.         /// Returns the URL for the SOAP endpoint using the context information available in the form
  9.         /// or HTML Web resource.
  10.         ///</summary>
  11.         var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
  12.         var serverUrl = "";
  13.         if (typeof GetGlobalContext == "function") {
  14.             var context = GetGlobalContext();
  15.             serverUrl = context.getServerUrl();
  16.         }
  17.         else {
  18.             if (typeof Xrm.Page.context == "object") {
  19.                 serverUrl = Xrm.Page.context.getServerUrl();
  20.             }
  21.             else { throw new Error("Unable to access the server URL"); }
  22.         }
  23.         if (serverUrl.match(/\/$/)) {
  24.             serverUrl = serverUrl.substring(0, serverUrl.length - 1);
  25.         }
  26.         return serverUrl + OrgServicePath;
  27.     },
  28.     CloseIncidentRequest: function (incidentId, subject, resolutionTypeValue, durationValue, description) {
  29.  
  30.         var requestMain = ""
  31.         requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
  32.         requestMain += "  <s:Body>";
  33.         requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
  34.         requestMain += "      <request i:type=\"b:CloseIncidentRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
  35.         requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
  36.         requestMain += "          <a:KeyValuePairOfstringanyType>";
  37.         requestMain += "            <c:key>IncidentResolution</c:key>";
  38.         requestMain += "            <c:value i:type=\"a:Entity\">";
  39.         requestMain += "              <a:Attributes>";
  40.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  41.         requestMain += "                  <c:key>incidentid</c:key>";
  42.         requestMain += "                  <c:value i:type=\"a:EntityReference\">";
  43.         requestMain += "                    <a:Id>" + incidentId + "</a:Id>";
  44.         requestMain += "                    <a:LogicalName>incident</a:LogicalName>";
  45.         requestMain += "                    <a:Name i:nil=\"true\" />";
  46.         requestMain += "                  </c:value>";
  47.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  48.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  49.         requestMain += "                  <c:key>subject</c:key>";
  50.         requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + subject + "</c:value>";
  51.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  52.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  53.         requestMain += "                  <c:key>description</c:key>";
  54.         requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + description + "</c:value>";
  55.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  56.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  57.         requestMain += "                  <c:key>timespent</c:key>";
  58.         requestMain += "                  <c:value i:type=\"d:int\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + durationValue + "</c:value>";
  59.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  60.         requestMain += "              </a:Attributes>";
  61.         requestMain += "              <a:EntityState i:nil=\"true\" />";
  62.         requestMain += "              <a:FormattedValues />";
  63.         requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
  64.         requestMain += "              <a:LogicalName>incidentresolution</a:LogicalName>";
  65.         requestMain += "              <a:RelatedEntities />";
  66.         requestMain += "            </c:value>";
  67.         requestMain += "          </a:KeyValuePairOfstringanyType>";
  68.         requestMain += "          <a:KeyValuePairOfstringanyType>";
  69.         requestMain += "            <c:key>Status</c:key>";
  70.         requestMain += "            <c:value i:type=\"a:OptionSetValue\">";
  71.         requestMain += "              <a:Value>" + resolutionTypeValue + "</a:Value>";
  72.         requestMain += "            </c:value>";
  73.         requestMain += "          </a:KeyValuePairOfstringanyType>";
  74.         requestMain += "        </a:Parameters>";
  75.         requestMain += "        <a:RequestId i:nil=\"true\" />";
  76.         requestMain += "        <a:RequestName>CloseIncident</a:RequestName>";
  77.         requestMain += "      </request>";
  78.         requestMain += "    </Execute>";
  79.         requestMain += "  </s:Body>";
  80.         requestMain += "</s:Envelope>";
  81.         var req = new XMLHttpRequest();
  82.         req.open("POST", Sdk.Tools._getServerUrl(), false)
  83.         // Responses will return XML. It isn't possible to return JSON.
  84.         req.setRequestHeader("Accept", "application/xml, text/xml, */*");
  85.         req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  86.         req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
  87.         req.send(requestMain);
  88.  
  89.         //work with response here
  90.         return req.responseXML.xml;
  91.     },
  92.     __namespace: true
  93. };

Enjoy yourself,

N.JL

No comments:

Post a Comment