Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Answered

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

Posted on by 1,579

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);  
    }
}

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: Is it possible to use JavaScript to Execute a Real Time On-Demand Workflow (NOT an Asynchronous Workflow)

    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!

  • Verified answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Is it possible to use JavaScript to Execute a Real Time On-Demand Workflow (NOT an Asynchronous Workflow)

    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?

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,647 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans