web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Workflow Id using JS

(1) ShareShare
ReportReport
Posted on by 12,119 Moderator

Hi all

I am using the following code to get the workflow Id but my code is not working

function getWorkflowId() {
    debugger;
    var workflowName = "List";//"0197a5a2-e574-49ac-8511-f1b50954ca52"
    var odataSelect = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + workflowName + '\'";
 
    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 workFlowID = xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
    }
}

Please suggest me Where am I wrong?

Thank You

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Rajkumar Rajaraman Profile Picture
    on at

    Hi Abdul,

    Check your workflow name.

    Refer this:

    columbuscrm.blogspot.in/.../get-workflow-guid-in-javascript-using.html

    Hope this helps.

    Regards,

    Rajkumar Rajaraman

    "Please verified if you found useful"

  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at

    use Xrm.Page.context.getClientUrl(); instead of Xrm.Page.context.getServerUrl()

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Your main error is that you're trying to parse xml of response but response is JSon. Your code should look like following:

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=Name eq 'test%20name' and  ParentWorkflowId eq null and StateCode/Value eq 1", true);

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           this.onreadystatechange = null;

           if (this.status === 200) {

               var returned = JSON.parse(this.responseText).d;

               var results = returned.results;

               for (var i = 0; i < results.length; i++) {

                   var workflowId = results[i].WorkflowId;

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    I used this tool for generation of query and sampling of JavaScript - github.com/.../CRMRESTBuilder

    Try it - it's really helpfull.

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Abdul Wahab,

    Try the code below.

    function GetWorkflowIDByName(wfName) {
    
        var serverUrl = Xrm.Page.context.getServerUrl();
        var wfID = null;
        //Prepare ODATA query to fetch WF GUID by WF Name
        var odataSelect = serverUrl + "/xrmservices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=ActiveWorkflowId/Id ne null and Name eq '" + wfName + "'";
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {            
                if (data.d != null && data.d.results != null && data.d.results.length != 0 && data.d.results[0].WorkflowId != null) {
                    wfID = data.d.results[0].WorkflowId;
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
        });
    } 

    Hope this helps.

  • Verified answer
    Alagunellaikumar Profile Picture
    6,212 on at

    HI

    Pls refer below URL

    rajeevpentyala.com/.../workflow-id-by-name

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans