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)

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

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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.

  • Sven Peeters Prodware Profile Picture
    657 on at

    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
    sandeepstw Profile Picture
    4,601 on at

    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

    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
    gdas Profile Picture
    50,091 Moderator on at

    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.

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