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 CRM (Archived)

Wait for result of Xrm.WebApi.online.execute Calling a custom Action

(0) ShareShare
ReportReport
Posted on by 657

Hi,

I defined a custom action which returns a boolean. I would like to create a javascript method that calls the action and returns the result.

This javascript method will then be used as an 'Enable Rule' in the ribbon.

I'm not that great in JScript and fairly new to promises.

The problem is that I have 2 async operations that need to be 'awaited' and the result of the 2th should be returned as method result.

Imaging that this part of code is in a JScript method, and I would like this method to return the result of the 2th operation -> response.IsKiamoEnabled

I have no clue on how I could achieve this properly (except using XMLHttpRequest with async = false)

        var isKiAMOEnabled = false;

        // Use the request object to execute the function
        Xrm.WebApi.online.execute(request).then(
            function (result) {
                if (result.ok) {                   
                    result.json().then(function (response)
                    {
                        console.log("pw_IsUserKIAMOEnabled returned " + response.IsKiamoEnabled)
                        return response.IsKiamoEnabled;
                    });
                }               
                return tmpisKiAMOEnabled;
            },
            function (error) {               
                console.log(error.message);
                return false;
            }
        );

       
        return isKiAMOEnabled;

Any help would be greatly appreciated …

Regards,

Sven Peeters

*This post is locked for comments

I have the same question (0)
  • Verified answer
    aindike Profile Picture
    144 on at

    Hi,

    According to my knowledge withing Promise patten, we can not return a value to the outside variable. So It is better this kind of solution

    RetrieveEntityByIdWebApi: function RetrieveEntityByIdWebApi(RetriveString) {

    var result;

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + RetriveString, false);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function () {

    if (this.readyState === 4) {

    req.onreadystatechange = null;

    if (this.status === 200) {

    result = JSON.parse(this.response);

    } else {

    Xrm.Utility.alertDialog(this.statusText);

    }

    }

    };

    req.send();

    return result;

    }

    More: docs.microsoft.com/.../query-data-web-api

    Thanks

  • Suggested answer
    ARIFNIIT Profile Picture
    1,391 on at

    awa04 is right, you can use XMLHttpRequest to return retrieved data.

  • Sven Peeters Prodware Profile Picture
    657 on at

    Thank you for the confirmation, this is how I solved it for now ...

  • Suggested answer
    gangwar.deepak Profile Picture
    20 on at

    You can use following code to wait for the response

    Xrm.WebApi.online.execute(req).then(

           function (data) { if(data.ok){

               data.json().then( function (response){

               console.log(response);})}

           },

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans