locked
CALLING WORKFLOW FROM JAVASCRIPT. RRS feed

  • Question

  • Hi,

    I used your code and trying to call my workflow, but getting error message of 500. Any solutions to this please? Code attached.

    function RunWorkflow() {

       

    varentityId = Xrm.Page.data.entity.getId().substring(1, 37); //Guid of record that workflow is to run on.


       

    varworkflowId = "CD27EB5A-E644-E511-80DE-00155D003900"; //Workflow Guid.


      

       

    varurl = Xrm.Page.context.getServerUrl();

       

    varOrgServicePath = "/XRMServices/2011/Organization.svc/web";

        url = url + OrgServicePath;

       

    varrequest;

        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:ExecuteWorkflowRequest\" 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>EntityId</c:key>"+

               

    "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">"+ entityId + "</c:value>"+

             

    "</a:KeyValuePairOfstringanyType>"+

             

    "<a:KeyValuePairOfstringanyType>"+

               

    "<c:key>WorkflowId</c:key>"+

               

    "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">"+ workflowId + "</c:value>"+

             

    "</a:KeyValuePairOfstringanyType>"+

           

    "</a:Parameters>"+

           

    "<a:RequestId i:nil=\"true\" />"+

           

    "<a:RequestName>ExecuteWorkflow</a:RequestName>"+

         

    "</request>"+

       

    "</Execute>"+

     

    "</s:Body>"+


    "</s:Envelope>"

    ;

       

    varreq = newXMLHttpRequest();

        req.open(

    "POST", url, true)

       

    // Responses will return XML. It isn't possible to return JSON.


        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.onreadystatechange =

    function() { assignResponse(req); };

        req.send(request);

     

    }



    function

    assignResponse(req) {

       

    if(req.readyState == 4) {

            alert(req.status);

           

    if(req.status == 200) {

               

    //alert('successfully executed the workflow');


            }

        }

    }

    Monday, September 28, 2015 9:04 AM

All replies

  • Monday, September 28, 2015 11:52 AM
    Answerer
  • We also faced problem with this requirement but we used bellow code it works fine. 

    function processWorkFlow(workflowId) { 
    	var url = Xrm.Page.context.getClientUrl();
    	var entityId = Xrm.Page.data.entity.getId();
        //var _return = window.confirm('Are you want to execute workflow.');
        //if (_return) {
            url = url +"/XRMServices/2011/Organization.svc/web";
            var request;
            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:ExecuteWorkflowRequest\" 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>EntityId</c:key>" +
                                    "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + entityId + "</c:value>" +
                                  "</a:KeyValuePairOfstringanyType>" +
                                  "<a:KeyValuePairOfstringanyType>" +
                                    "<c:key>WorkflowId</c:key>" +
                                    "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + workflowId + "</c:value>" +
                                  "</a:KeyValuePairOfstringanyType>" +
                                "</a:Parameters>" +
                                "<a:RequestId i:nil=\"true\" />" +
                                "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
                              "</request>" +
                            "</Execute>" +
                          "</s:Body>" +
                        "</s:Envelope>";
    
            var req = new XMLHttpRequest();
            req.open("POST", url, true)
            // Responses will return XML. It isn't possible to return JSON.
            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.onreadystatechange = function () { assignResponse(req); };
            req.send(request);
        }
    function assignResponse(req,entityId) {
        if (req.readyState == 4) {
            if (req.status == 200) {
                Xrm.Page.data.refresh();   }
        }
    }

    I Hope I could help. If I have answered please mark as 'Answer'. If was just helpful, please vote. Thanks and happy coding!

    https://mydynamicsmscrm.wordpress.com/


    Wednesday, October 7, 2015 4:48 AM