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 365 | Integration, Dataverse...
Answered

Is it possible to use JavaScript to Execute a Real Time On-Demand Workflow (NOT an Asynchronous Workflow)

(0) ShareShare
ReportReport
Posted on by 1,589

Is it possible to use JavaScript to Execute a Real Time On-Demand Workflow (NOT and Asynchronous Workflow)

Using the code below, I am able to execute Asynchronous On-Demand Workflows.

However, I would like to execute a realtime workflow via JavaScript.

Is that possible? If yes, how would I do that via JavaScript?

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   "\"}");
                IsDirty = formContext.data.entity.getIsDirty();
                if (IsDirty) {
                    formContext.data.refresh().then(function () {
                        formContext.data.refresh(true);
                    }, null);
                        formContext.data.refresh(true);
                }
                else {
                    //formContext.data.refresh(true);
                    formContext.data.refresh().then(function () {
                        formContext.data.refresh(true);
                    }, null);
                        formContext.data.refresh(true);
                }
        } 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   "\"}");
                IsDirty = formContext.data.entity.getIsDirty();
                if (IsDirty) {
                    formContext.data.refresh().then(function () {
                        formContext.data.refresh(true);
                    }, null);
                        formContext.data.refresh(true);
                }
                else {
                    //formContext.data.refresh(true);
                    formContext.data.refresh().then(function () {
                        formContext.data.refresh(true);
                    }, null);
                        formContext.data.refresh(true);
                }
        }

    } else if (OptVal == 100000001); {
        formContext.getAttribute("ace_serviceaddressline1").setValue(null);
        formContext.getAttribute("ace_serviceaddressline2").setValue(null);
        formContext.getAttribute("ace_serviceaddresscity").setValue(null);
        formContext.getAttribute("ace_serviceaddressstate").setValue(null);
        formContext.getAttribute("ace_serviceaddresspostalcode").setValue(null);
        formContext.getAttribute("ace_serviceaddresscountry").setValue("United States");
        formContext.getAttribute("ace_serviceaddresslatitude").setValue(null);
        formContext.getAttribute("ace_serviceaddresslongitude").setValue(null);  
    }
}

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Jim,

    Code for the executing sync workflow should be pretty the same as for async workflow. Have you tried it and if it doesn't work - what kind of errors did you experience?

  • ACECORP Profile Picture
    1,589 on at

    a33ik you are correct! Very strange D365 behavior has befallen my development instance the past few days. Now I do not get any errors when running realtime workflows via script. I do however get logged out every 3 minutes.. but I have a support ticket open with Microsoft on that. All the craziness began a few days ago. Not sure why. Thanks for your help!

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 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 51

#2
Martin Dráb Profile Picture

Martin Dráb 39 Most Valuable Professional

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 26 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans