locked
How to execute workflow through javascript in CRM 2013 RRS feed

  • Question

  • Hi,

    I have created a business process and I want to execute workflow on custom button through javascript. I tried this script but this script does not run after message box show. Please tell me how to execute my workflow through javascript

    function RunWorkflow() {
        var _return = window.confirm('Are you want to execute workflow.');
        if (_return) {
            var url = Xrm.Page.context.getServerUrl();
            var entityId = Xrm.Page.data.entity.getId();
            var workflowId = '541B45C9-3F88-4448-9690-2D4A365C3172';
            var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
            url = url + OrgServicePath;
            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) {
        if (req.readyState == 4) {
            if (req.status == 200) {
                alert('successfully executed the workflow');
            }
        }
    }

    Thursday, January 8, 2015 10:49 AM

All replies

  • Hi,

    Use the code given in below link for the confirmation alert in place of window.confirm and check.

    http://www.magnetismsolutions.co.nz/blog/roshanmehta/2013/10/02/crm-2013-javascript-alerts-and-confirmations

    If still the issue exists use the code given in below link to call the workflow.

    http://ankit.inkeysolutions.com/2013/03/dynamics-crm-2011-trigger-workflow-from.html

    Thanks,

    Prasad

    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question


    Thursday, January 8, 2015 11:26 AM
  • If I just call business process workflow on custom button click then record automatically transfer from one entity to another entity like defined in Business Process.
    Thursday, January 8, 2015 11:33 AM
  • function ConfirmAndCall(entityId, workflowProcessId) { Xrm.Utility.confirmDialog("Are you sure you want to Call the Process?", function (entityId, workflowProcessId) { // your Code }, function () { // your Code }); }

    Is this how you have tried it? and still its not working.

    Thanks,

    Prasad

    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question

    Thursday, January 8, 2015 11:47 AM
  • Its not working :(
    Thursday, January 8, 2015 11:51 AM
  • Replace Xrm.Page.context.getServerUrl

    to

    Xrm.Page.context.getClientUrl

    

    Regards Faisal

    Thursday, January 8, 2015 2:07 PM
  • Hi,
    I used this script for execute the workflow but its not working. When I used alert(req.status); then 500 show. Please tell me what did I mistake in my script.

    function RunWorkflow() {
        var _return = window.confirm('Are you want to execute workflow.');
        if (_return) {
            var url = Xrm.Page.context.getClientUrl();
            var entityId = Xrm.Page.data.entity.getId();
            var workflowId = '541B45C9-3F88-4448-9690-2D4A365C3172';
            var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
            url = url + OrgServicePath;
            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) {
        if (req.readyState == 4) {

    alert(req.status);

            if (req.status == 200) {
                alert('successfully executed the workflow');
            }
        }
    }

    Friday, January 9, 2015 9:18 PM