Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

(0) ShareShare
ReportReport
Posted on by 1,583

The code below executes several Asynchronous On-Demand Workflows within the appropriate if/else section. 

I was forced to hardcode the Guid of each workflow directly in the code due to time constraints. 

Ideally, I would like to get the Guid of the workflow by its name. 

How would I do that exactly within the context of the code below?

function setOpportunityServiceAddress(executionContext) {
    var formContext = executionContext.getFormContext();
    var CurrentRecordId = formContext.data.entity.getId();
    CurrentRecordId = CurrentRecordId.replace("{", "").replace("}", "");
    var customer = formContext.getAttribute("ace_customer").getValue();
    var customerType = customer[0].entityType;
    var OptVal = formContext.getAttribute("ace_serviceaddress").getValue();
    var WkflowId = "";
    var clientUrl = "";
    var requestUri = "";
    var xhr = "";
    var IsDirty = "";
    if (OptVal == 100000000) {
        if (customerType == "contact") {
            //WkflowId = GetWorkflowIDByURL(executionContext, "/api/data/v9.1/workflows?$select=workflowidunique&$filter=(name eq 'Opportunity - (On Demand) Copy Related Contact Address to Service Address' and statuscode eq 2)&$top=1");
            WkflowId = "fc67a661-9e41-49b5-b1ca-79eaaf9eadd4";
            clientUrl = formContext.context.getClientUrl();
            requestUri = clientUrl   "/api/data/v9.0/workflows("   WkflowId   ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
            xhr = new XMLHttpRequest();
            xhr.open("POST", requestUri, true);
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            xhr.setRequestHeader("OData-MaxVersion", "4.0");
            xhr.setRequestHeader("OData-Version", "4.0");
            xhr.onreadystatechange = function () {
                if (this.readyState == 4) {
                    xhr.onreadystatechange = null;
                    if (this.status == 200) {
                        var result = JSON.parse(this.response);
                    } 
                        else {
                            var error = JSON.parse(this.response).error;
                        }
                }
            };
            xhr.send("{\"EntityId\":\""   CurrentRecordId   "\"}");
                
        } else if (customerType == "account") {
            //WkflowId = GetWorkflowIDByURL(executionContext, "/api/data/v9.1/workflows?$select=workflowidunique&$filter=(name eq 'Opportunity - (On Demand) Copy Related Account Address to Service Address' and statuscode eq 2)&$top=1");
            WkflowId = "9e5b668c-2feb-4d92-abfa-5af7194a02df";
            clientUrl = formContext.context.getClientUrl();
            requestUri = clientUrl   "/api/data/v9.0/workflows("   WkflowId   ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";
            xhr = new XMLHttpRequest();
            xhr.open("POST", requestUri, true);
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            xhr.setRequestHeader("OData-MaxVersion", "4.0");
            xhr.setRequestHeader("OData-Version", "4.0");
            xhr.onreadystatechange = function () {
                if (this.readyState == 4) {
                    xhr.onreadystatechange = null;
                    if (this.status == 200) {
                        var result = JSON.parse(this.response);
                    } 
                        else {
                            var error = JSON.parse(this.response).error;
                        }
                }
            };
            xhr.send("{\"EntityId\":\""   CurrentRecordId   "\"}");
        }

    } else if (OptVal == 100000001); {
       ////DO SOME STUFF
    }
}

  • ACECORP Profile Picture
    ACECORP 1,583 on at
    RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

    Fantastic! I learned a ton from you on this. Thank you very much for all of your assistance!

  • RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

    Hi Partner,

    Has the problem been solved? Any updates?

    Please click Yes under "Did this answer your question?" to close this thread.

    pastedimage1606976309138v1.png

    Thanks. 

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Verified answer
    RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

    Hi Jim,

    It may a little mistake of the tool, you can manually change statuscode value in the code.

    And you can use: statuscode eq 2

    Just like following:

    XXXXXXXX.crm.dynamics.com/.../workflows eq 'test%20opp%20workflow' and _parentworkflowid_value eq null and statuscode eq 2

    Test result:

    pastedimage1606891190388v1.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • ACECORP Profile Picture
    ACECORP 1,583 on at
    RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

    Thanks. I was able to do that in the Rest Builder but there appears to be a glitch.

    Screen-Shot-2020_2D00_12_2D00_01-at-10.08.47-PM.png

    Instead of statuscode = activated or 1 or whatever it should be, the rest bulder produces statuscode eq eq as shown below.

    Screen-Shot-2020_2D00_12_2D00_01-at-10.13.00-PM.png

    Xrm.WebApi.online.retrieveMultipleRecords("workflow", "?$select=workflowid,workflowidunique&$filter=name eq 'Opportunity - (On Demand) Copy Related Contact Address to Service Address' and  _parentworkflowid_value eq null and  statuscode eq eq").then(
        function success(results) {
            for (var i = 0; i < results.entities.length; i  ) {
                var workflowid = results.entities[i]["workflowid"];
                var workflowidunique = results.entities[i]["workflowidunique"];
            }
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );

    What should it be?

    If I manually change it to statuscode = 1 and try to execute it in my browser before taking it to code as shown below...

    Screen-Shot-2020_2D00_12_2D00_01-at-10.14.35-PM.png

    .... I get nothing back as shown below

    Screen-Shot-2020_2D00_12_2D00_01-at-10.14.43-PM.png

    If I remove the statuscode = 1 and execute that, I get 2 workflows back...

    Screen-Shot-2020_2D00_12_2D00_01-at-10.16.47-PM.png

    What does that last part need to be of the code with respect to statuscode = activated or statuscode = 1 exactly to execute properly?

  • Verified answer
    RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

    Hi Jim,

    You can add filter, just like following screenshot shown:

    pastedimage1606877426866v1.png

    pastedimage1606877516439v2.png

    Excute code, now just one result and the workflowid is correct.

    3348.pastedimage1606876996017v8.png

    And it is original results, it return two results.

    pastedimage1606877629786v3.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • ACECORP Profile Picture
    ACECORP 1,583 on at
    RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

     

    When I use the rest builder I get back three workflows as shown below. If I further filter by statecode = Active I get back two. The funny thing is I have only 1 workflow with the name I am querying by so I do not understand why that one workflow shows up as 2 with statecode = Active.

    0:{entities:[0:{@odata.etag:"W/"19655343"",workflowid:"fdd51e2c-3734-eb11-a813-000d3a5a7103",workflowidunique:"06f60cd6-ad33-4ce5-a9ba-9664d62260eb"},1:{@odata.etag:"W/"19655329"",workflowid:"81d57fe1-f931-eb11-a813-000d3a5a7ad8",workflowidunique:"cd6f1b6f-fe66-433e-a1b0-f04c168d6194"},2:{@odata.etag:"W/"19655369"",workflowid:"fc67a661-9e41-49b5-b1ca-79eaaf9eadd4",workflowidunique:"126382c3-fa59-4fe0-9551-5c692c25954a"}]}

    I get the same result of two returned workflows with XRMToolbox as well. How can I make sure on the correct one is returned or how do I know which one is the correct so I can make my code only care about the correct Guid?

  • Verified answer
    RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

    Hi Jim,

    You can use web Api to retrieve workflow with the name as filter:

    You can download & install Rest Builder tool to build the web api query-

    Refer this blog: https://carldesouza.com/dynamics-crm-rest-builder/

    You can download the tool here: https://github.com/jlattimer/CRMRESTBuilder/releases

    1.create web api:

     pastedimage1606812117339v1.png

    2.excute:

     pastedimage1606812172892v2.png

    3.Test:

    pastedimage1606812235706v3.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Microsoft Profile Picture
    Microsoft on at
    RE: How can I Get the Guid of a Workflow via Unified Interface D365 V9.1 Compliant JavaScript from the Workflow's Name

    In dynamics 365, workflow are stored in SQL in table: WorkflowTable, there are fields like Name and Recid.

    do you mean to find the Recid value 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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans