Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

WEB Api + General JScript question

(0) ShareShare
ReportReport
Posted on by 657

Hi All,

Since CRM 2016 we got this new WEBAPI that will replace the SOAP endpoint.

The community already spent a lot of effort creating helper files to make our life easier.

Being a C# and not a JScript developer, I'm a bit stuck on one part of the concept.

I do understand the importance of async handling of these odata calls.

But I can't get my head around a simple function I need to create.

I have one function CanApprove that will have to return false or true depending on some criteria (which is not important for this question).

This is will manage the visibility of the button via an EnableRule of the Ribbon Workbench.

How do I make the function 'wait' until the odata call returns , and only then return the value 'true' or 'false'.

I could define a global boolean 'CallFinished' + result variable and make the CanApprove wait with a while CallFinished = false.

In the success or error function of the odata call, I would then set the result / CallFinished variable.

But I hope there are other and cleaner solutions to achieve my goal.

Since synchronious xdr and XMLHttpRequest is depreciated, I better not resort to that solution either.

Very curious how this is tackeld in the field ...

Regards,

Sven Peeters

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: WEB Api + General JScript question

    Asynchronous call does not give you expected result using return statement in JS function.

    For your scenario I would suggest create a field where you can pre store the expected result using odata at the time of creation of record. Based on the field value using enable rule show hide the button.

  • Sven Peeters Prodware Profile Picture
    657 on at
    RE: WEB Api + General JScript question

    Hi Sandeep,

    XmlHttpRequest synchronious is a deprecated feature in some browser since a few months. So I'm not sure if we should use synchr request ...

  • Suggested answer
    sandeepstw Profile Picture
    4,601 on at
    RE: WEB Api + General JScript question

    Hi Sven,

    Use Asynchronous Data Access Methods, when you access data by using the Microsoft Dynamics CRM web services that use the REST or SOAP endpoint for web resources, always use an XMLHttpRequest that is configured to execute asynchronously. The reason is that the browser operates on a single thread. If that thread is being used to execute a long-running process synchronously the browser will stop responding.

    This is case when you are going to use long-running process. But in case when you need to get simple call I will suggest to use synchronously.

  • Sven Peeters Prodware Profile Picture
    657 on at
    RE: WEB Api + General JScript question

    Hi Goutam,

    So because async is 'better' we have to make such ugly code constructions with timeouts and stuff.

    Not really my preferred way but it's an option yes.

    Currently just keeping it simple with sync instead of async, if nobody has a cleaner solution then I'll have to implement that :-(

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: WEB Api + General JScript question

    Hello,

    I have seen in the community lot of people are raising same issue.

    There is two options either you have to use old fashion using SDK.Rest procedure and the second option is using settimeout. You can declare the Boolean variable at the top as null and then check the value inside timeout until and unless some value assigned to the  variable.

            function ShowHideButton() {
                //Declare Variable
                var BooleanResult = null;
                //Call Web API and set BooleanResult the value in the onsuccess
    
    
                //---
    
                var intervalId = window.setInterval(
                function () {
                    if (BooleanResult != null) {
                        window.clearInterval(intervalId);
                    }
                }, 500);
    
    
                return BooleanResult;
            }


    Hope this helps.

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 the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans