locked
Call Workflow from webresource RRS feed

  • Question

  • Hi,

    I want to call workflow from CRM webresource which is called on Page load.

    Please guide me regarding same.

    Thanks in advance.

    Thanks

    Monday, April 21, 2014 12:55 PM

All replies

  • Use something like this to execute a workflow from JavaScript. If you are running from inside a web resource, you might need to add "window.parent" before any of the Xrm.Page references. 

    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

    Monday, April 21, 2014 1:02 PM
    Moderator
  • Hi Jason Lattimer,

    But how to get entity id on page load.

    Thanks



    Monday, April 21, 2014 1:41 PM
  • Xrm.Page.data.entity.getId()

    Is the standard way of getting the entity id of a record - this function I posted would handle that already. 

    If you need something to happen when the record is first loaded you would need to create a function that can be attached to the OnLoad event which calls ExecuteWorkflowSOAP - assuming also of course you've set the proper Workflow Id (replace 99999999-9999-9999-9999-999999999999)


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, April 21, 2014 2:10 PM
    Moderator
  • Hi,

    we can get entity id  after create the record. right?

    But how can we get entity id while opening the form???

    Monday, April 21, 2014 2:13 PM
  • Right you need to save the record before the id is generated. Otherwise getId() returns an empty guid (all 0's)

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, April 21, 2014 3:13 PM
    Moderator