Get Workflow Guid in Javascript using OData query

I find that using workflow id might be problematic across deployment, so I wrote a function to retrieve workflow’s guid given the workflow’s name. However, please take some care in assigning workflow name to avoid pain.

function call()
{
     
alert("Tets");
var name="Create Phone Call Via Js";
alert(name);
var wf=getWorkflowId(name);
alert(wf);

 }

function getWorkflowId(workflowName) {
var aa=workflowName;
var entityId = Xrm.Page.data.entity.getId();
alert(entityId);
alert(workflowName);

var serverUrl = Xrm.Page.context.getClientUrl();
alert(serverUrl);

var odataSelect = serverUrl + "/xrmservices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq 'Create Phone Call Via Js'";

alert(odataSelect);

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", odataSelect, false);
    xmlHttp.send();

    if (xmlHttp.status == 200) {
        var result = xmlHttp.responseText;

        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(result);
         
        return xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
    }
}

Hope it Help!!

Comments

  1. Excellent post, thank you. You saved me a good few hours of work.

    ReplyDelete

Post a Comment

Popular posts from this blog

Workflow Activity Input and Output Parameters and Data Types in MS CRM 2013/15/16

Create a custom Grid in MS Dynamic CRM using Jquery DataTable