locked
Triggering workflow from javascript crm 2011 RRS feed

  • Question

  • Hi,

    I've found a lot of examples how to Execute workflow from javascript, but any of them doesn't work.

    Have anyone solution to execute worfklow ?

    Wednesday, February 5, 2014 2:58 PM

Answers

  • Try this:

    function ExecuteWorkflowSOAP() {
        var workflowId = "99999999-9999-9999-9999-999999999999";
        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: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/'>" + Xrm.Page.data.entity.getId() + "</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", 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.onreadystatechange = function () {};
        req.send(request.join(""));
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, February 5, 2014 3:00 PM
    Moderator

All replies

  • Try this:

    function ExecuteWorkflowSOAP() {
        var workflowId = "99999999-9999-9999-9999-999999999999";
        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: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/'>" + Xrm.Page.data.entity.getId() + "</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", 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.onreadystatechange = function () {};
        req.send(request.join(""));
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, February 5, 2014 3:00 PM
    Moderator
  • Unfortunatelly doesn't work.
    Wednesday, February 5, 2014 3:13 PM
  • Did you change the workflowId to the correct value?

    I just verified and the code does indeed work. You would also have to run from inside a record or change getId() to a valid record id. 


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, February 5, 2014 3:29 PM
    Moderator
  • Run OnSave on Task form.

    Yes, I did change workflowid (workflowinstance)

    Wednesday, February 5, 2014 3:33 PM
  • It is not the Workflow Instance - it is the id of the actual workflow.

    You can find this by opening the workflow record from the Process area and looking in the URL. 

    https://test.dynamics.com/sfa/workflow/edit.aspx?id=%7b366EC19E-3B42-4952-96A7-3A0E093C1BEA%7d


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, February 5, 2014 3:38 PM
    Moderator
  • OMG...

    Yes, it works :)

    thx

    Wednesday, February 5, 2014 4:21 PM
  • Awesome work Jason!
    Tuesday, March 18, 2014 7:58 AM