locked
SetState Resolved Incident Javascript RRS feed

  • Question

  • Hi!

    I'm trying to Resolve an incident(Case) With Javascript... changed the ribbon button to execute my customized "resolve" function. I get "Internal server error".

    Is it possible to set the state/status to 1/5? Please help me if you can:-)

    BR Knut

    My.incident.resolveCase = function (entityName, entityId) {
        
       var newstatusCode=-5;
       var newstateCode=1;
        // create the SetState request
        var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        request += "<s:Body>";
        request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
        request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<c:key>EntityMoniker</c:key>";
        request += "<c:value i:type=\"a:EntityReference\">";
        request += "<a:Id>" + entityId + "</a:Id>";
        request += "<a:LogicalName>" + entityName +  "</a:LogicalName>";
        request += "<a:Name i:nil=\"true\" />";
        request += "</c:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<c:key>State</c:key>";
        request += "<c:value i:type=\"a:OptionSetValue\">";
        request += "<a:Value>" + newstateCode + "</a:Value>";
        request += "</c:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<c:key>Status</c:key>";
        request += "<c:value i:type=\"a:OptionSetValue\">";
        request += "<a:Value>" + newstatusCode + "</a:Value>";
        request += "</c:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "</a:Parameters>";
        request += "<a:RequestId i:nil=\"true\" />";
        request += "<a:RequestName>SetState</a:RequestName>";
        request += "</request>";
        request += "</Execute>";
        request += "</s:Body>";
        request += "</s:Envelope>";
    
        //send set state request
    jQuery.support.cors = true;
        $.ajax({
            type: "POST",
            crossDomain: true,
            contentType: "text/xml; charset=utf-8",
            datatype: "xml",
            url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web",
            data: request,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
                XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                Xrm.Page.ui.close();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    
    };

     
    Monday, May 5, 2014 8:29 AM

Answers

  • Hi,

        To close the CASE(Incident) you have to use CloseIncidentRequest instead of using SetStateRequest

    use following code:

    //note: change “49fff4f3-efd1-e311-92c2-d89d6776bce0” to your CASE record guid below

    function closecase() {

        var statuscode=5;

        var entityid="incidentid";

        var entity = "incident";

        var request = ["<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>",

      "<s:Body>",

        "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>",

          "<request i:type='b:CloseIncidentRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>",

           "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",

              "<a:KeyValuePairOfstringanyType>",

                "<c:key>IncidentResolution</c:key>",

                "<c:value i:type='a:Entity'>",

                  "<a:Attributes>",

                    "<a:KeyValuePairOfstringanyType>",

                      "<c:key>"+ entityid +"</c:key>",

                      "<c:value i:type='a:EntityReference'>",

                        "<a:Id>49fff4f3-efd1-e311-92c2-d89d6776bce0</a:Id>",

                        "<a:LogicalName>" + entity + "</a:LogicalName>",

                        "<a:Name i:nil='true' />",

                      "</c:value>",

                    "</a:KeyValuePairOfstringanyType>",

                    "<a:KeyValuePairOfstringanyType>",

                      "<c:key>subject</c:key>",

                      "<c:value i:type='d:string' xmlns:d='http://www.w3.org/2001/XMLSchema'>TestCase Closed...</c:value>",

                    "</a:KeyValuePairOfstringanyType>",

                  "</a:Attributes>",

                 "<a:EntityState i:nil='true' />",

                  "<a:FormattedValues />",

                  "<a:Id>00000000-0000-0000-0000-000000000000</a:Id>",

                  "<a:LogicalName>incidentresolution</a:LogicalName>",

                  "<a:RelatedEntities />",

                "</c:value>",

              "</a:KeyValuePairOfstringanyType>",

              "<a:KeyValuePairOfstringanyType>",

                "<c:key>Status</c:key>",

                "<c:value i:type='a:OptionSetValue'>",

                  "<a:Value>"+ statuscode +"</a:Value>",

                "</c:value>",

              "</a:KeyValuePairOfstringanyType>",

            "</a:Parameters>",

            "<a:RequestId i:nil='true' />",

            "<a:RequestName>CloseIncident</a:RequestName>",

          "</request>",

        "</Execute>",

      "</s:Body>",

    "</s:Envelope>"

        ];

        var req = new XMLHttpRequest();

        req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);

        try {

            req.responseType = 'msxml-document';

        } catch (e) { }

        req.setRequestHeader("Accept", "application/xml, text/xml, */*");

        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");

        req.send(request.join(""));

        alert("case closed....");

       

    }


    • Proposed as answer by Chen Xiong Monday, May 5, 2014 10:26 AM
    • Edited by Gugan A Monday, May 5, 2014 11:15 AM
    • Marked as answer by knutmyre Monday, May 5, 2014 1:59 PM
    Monday, May 5, 2014 10:06 AM

All replies

  • Hi,

        To close the CASE(Incident) you have to use CloseIncidentRequest instead of using SetStateRequest

    use following code:

    //note: change “49fff4f3-efd1-e311-92c2-d89d6776bce0” to your CASE record guid below

    function closecase() {

        var statuscode=5;

        var entityid="incidentid";

        var entity = "incident";

        var request = ["<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>",

      "<s:Body>",

        "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>",

          "<request i:type='b:CloseIncidentRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>",

           "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",

              "<a:KeyValuePairOfstringanyType>",

                "<c:key>IncidentResolution</c:key>",

                "<c:value i:type='a:Entity'>",

                  "<a:Attributes>",

                    "<a:KeyValuePairOfstringanyType>",

                      "<c:key>"+ entityid +"</c:key>",

                      "<c:value i:type='a:EntityReference'>",

                        "<a:Id>49fff4f3-efd1-e311-92c2-d89d6776bce0</a:Id>",

                        "<a:LogicalName>" + entity + "</a:LogicalName>",

                        "<a:Name i:nil='true' />",

                      "</c:value>",

                    "</a:KeyValuePairOfstringanyType>",

                    "<a:KeyValuePairOfstringanyType>",

                      "<c:key>subject</c:key>",

                      "<c:value i:type='d:string' xmlns:d='http://www.w3.org/2001/XMLSchema'>TestCase Closed...</c:value>",

                    "</a:KeyValuePairOfstringanyType>",

                  "</a:Attributes>",

                 "<a:EntityState i:nil='true' />",

                  "<a:FormattedValues />",

                  "<a:Id>00000000-0000-0000-0000-000000000000</a:Id>",

                  "<a:LogicalName>incidentresolution</a:LogicalName>",

                  "<a:RelatedEntities />",

                "</c:value>",

              "</a:KeyValuePairOfstringanyType>",

              "<a:KeyValuePairOfstringanyType>",

                "<c:key>Status</c:key>",

                "<c:value i:type='a:OptionSetValue'>",

                  "<a:Value>"+ statuscode +"</a:Value>",

                "</c:value>",

              "</a:KeyValuePairOfstringanyType>",

            "</a:Parameters>",

            "<a:RequestId i:nil='true' />",

            "<a:RequestName>CloseIncident</a:RequestName>",

          "</request>",

        "</Execute>",

      "</s:Body>",

    "</s:Envelope>"

        ];

        var req = new XMLHttpRequest();

        req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);

        try {

            req.responseType = 'msxml-document';

        } catch (e) { }

        req.setRequestHeader("Accept", "application/xml, text/xml, */*");

        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");

        req.send(request.join(""));

        alert("case closed....");

       

    }


    • Proposed as answer by Chen Xiong Monday, May 5, 2014 10:26 AM
    • Edited by Gugan A Monday, May 5, 2014 11:15 AM
    • Marked as answer by knutmyre Monday, May 5, 2014 1:59 PM
    Monday, May 5, 2014 10:06 AM
  • Thanks mate:-)
    Monday, May 5, 2014 2:00 PM