Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Do not trigger workflow again until first step is executed

Posted on by 275

Hello Experts

I'm looking for a way to solve this issue, there is this workflow I'm calling using a ribbon button. It is working fine. But if the user clicks on the button for second time, the workflow is being kicked again. The workflow shouldn't trigger again until first step of workflow is executed. Is there a supported way I can achieve this? Below is the JavaScript code I'm using right now to call workflow using ribbon button. 

var isTriggered = false; 

function MoveMarketplaceOpptyToProposal() {


    Xrm.Utility.confirmDialog("Do you want to move the stage?", function () {
        SetValue("se_stage", 534340004);
        Xrm.Page.getAttribute("se_stage").setSubmitMode("always");
        Xrm.Page.data.save();

        if (!isTriggered) {
            executeWorkflows(Xrm.Page.data.entity.getId(), "FC39BB39-ABB0-4D1C-A515-AB63A94B7C4E");
        }
    }, function () { });
}

function executeWorkflows(recId, workflowId) {

    var clientUrl = Xrm.Page.context.getClientUrl();
    var query = "";
    try {

        //Define the query to execute the action
        query = "workflows(" + workflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";

        var data = {
            "EntityId": recId
        };

        //Create request
        var req = new XMLHttpRequest();
        req.open("POST", clientUrl + "/api/data/v8.2/" + query, false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");

        req.onreadystatechange = function () {

            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null;

                if (this.status == 200) {
                    //success callback this returns null since no return value available.
                    var result = JSON.parse(this.response);
                    isTriggered = true;

                } else {
                    //error callback
                    var error = JSON.parse(this.response).error;
                    isTriggered = false;
                }
            }
        };
        req.send(JSON.stringify(data));

    } catch (e) {
        alert("Error occured while executing a Workflow");
    }
}


Thanks in Advance.

*This post is locked for comments

  • Turbo Forms Profile Picture
    Turbo Forms 275 on at
    RE: Do not trigger workflow again until first step is executed

    Thanks a Lot Alex, I couldn't think a straight logic without code. This helped me a lot.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Do not trigger workflow again until first step is executed

    You might also add a condition to the workflow itself to basically do just the same (create a new field, and, then, in the first step of the workflow, check if "Workflow Running" field is set to true, then stop the workflow if it is. In the second step, update "Workflow Running" - set it to true. Just don't forget to flip it back to "false" in the last step(s) of your workflow)

  • Wayne Walton Profile Picture
    Wayne Walton 13,726 on at
    RE: Do not trigger workflow again until first step is executed

    You could disable the button after the first click, set a flag on a custom entity to keep the button disabled (have JS check on load for that field), and then have the workflow clear that flag when it should be clickable again.

    There's likely easier ways to do that, though.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans