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
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.
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 ...
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.
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 :-(
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.
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
52
Victor Onyebuchi
6