Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

execute workflow through webapi

(0) ShareShare
ReportReport
Posted on by 2,409

Hi, when executing workflow through javascript, is there a way to get a return value when it is done, otherwise, users get confused.

*This post is locked for comments

  • Suggested answer
    Summer Garg Profile Picture
    Summer Garg 585 on at
    RE: execute workflow through webapi

    Hi sdnd,

    Check this post, To execute workflow using Java-script......

    https://crmhub.blogspot.in/2018/04/execute-workflow-using-java-script-MS-Dynamic-CRM.html

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: execute workflow through webapi

    function executeWorkflow(accountId, workflowId, clientUrl) {

       var functionName = "executeWorkflow >>";

       var query = "";

       try {

           //Define the query to execute the action

           query = "workflows(" + workflowId.replace("}", "").replace("{", "") + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";

           var data = {          

                   "EntityId": accountId                        

           };

           //Create request

          // request url

          //org.crm.dynamics.com/.../workflows(“f0ca33cc-23fd-496f-80e1-693873a951ca”)/Microsoft.Dynamics.CRM.ExecuteWorkflow

           var req = new XMLHttpRequest();

           req.open("POST", clientUrl + "/api/data/v8.2/" + query, true);

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

                   } else {

                       //error callback

                       var error = JSON.parse(this.response).error;

                   }

               }

           };

           req.send(JSON.stringify(data));

       } catch (e) {

           throwError(functionName, e);

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: execute workflow through webapi

    Hi Andrii ,

    you are right but ... maybe it will be useful although it is not webapi; waiting for news from sdnd2000.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: execute workflow through webapi

    Francesco,

    It doesn't look like WebApi. Looks like Soap for me.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: execute workflow through webapi

    Hi sdnd2000,

    use these two simple functions:

    function RunCrmWorkflow(workflowid, entityid, name) {
        var runconfirm = window.confirm('Do you want to start "' + name + ' workflow"?');
        if (runconfirm) {
            var url = Xrm.Page.context.getServerUrl();
            var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
            url = url + OrgServicePath;
            var request;
            request = "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">" +
                          "<s:Body>" +
                            "<Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">" +
                              "<request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">" +
                                "<a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">" +
                                  "<a:KeyValuePairOfstringanyType>" +
                                    "<c:key>EntityId</c:key>" +
                                    "<c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">" + entityid + "</c:value>" +
                                  "</a:KeyValuePairOfstringanyType>" +
                                  "<a:KeyValuePairOfstringanyType>" +
                                    "<c:key>WorkflowId</c:key>" +
                                    "<c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">" + workflowid + "</c:value>" +
                                  "</a:KeyValuePairOfstringanyType>" +
                                "</a:Parameters>" +
                                "<a:RequestId i:nil=\"true\" />" +
                                "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
                              "</request>" +
                            "</Execute>" +
                          "</s:Body>" +
                        "</s:Envelope>";
    
            var req = new XMLHttpRequest();
            req.open("POST", url, true);
            req.setRequestHeader("Accept", "application/xml, text/xml, */*");
            req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");
            req.onreadystatechange = function () { RunCrmWorkflowAssignResponse(req); };
            req.send(request);
        }
    }
    
    function RunCrmWorkflowAssignResponse(req) {
        if (req.readyState == 4) {
            if (req.status == 200) {
                alert('Success.');
            } else {
                alert('Waiting...');
            }
        }
    }

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: execute workflow through webapi

    When it is sync then you just have to wait till operation is executed.

    When it's async I used approach similar to following - mscrm4ever.blogspot.com/.../crm-40-running-on-demand-workflow-from.html

  • sdnd2000 Profile Picture
    sdnd2000 2,409 on at
    RE: execute workflow through webapi

    it is sync, how to do it with async?

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: execute workflow through webapi

    Hello,

    Is your workflow sync or async?

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

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,375 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans