Microsoft CRM
How to make Xrm.WebApi calls synchronous in Dynamics 365/ CDS
Category: CDS,Dynamics 365,Microsoft CRM,Synchronous Xrm.WebApi calls Author: debajit prod Date: 3 weeks ago Comments: 0

This blog goes down as a request from my blog readers. And also a question from my clients whenever I take up an upgrade project and convert the existing XmlHttpRequest sync requests to Xrm.WebApi.

Before I proceed further, let me clear the air of confusion to begin with. There is no facility in dynamics WebApi to execute this as synchronous and rightfully so. After all the synchronous XmlHttp calls have already been deprecated in the browsers and soon they will cease to work.

But wait, if you have come this far you won’t be disappointed. I this blog I am going to explain on how you can execute Xrm.WebApi calls to execute in sync with few simple changes in the way you invoke them. You will be amazed to see how this simple trick can actually make the steps execute synchronous even with calls of Xrm.WebApi. So let’s start our journey now.

Below is a sample scenario. In the below code, “executeOnLoad” function is fired when the function registered on formload event of the record. Here the requirement is retrieveOperation1 should complete first, then statement 2 shall be execute and then finally the retrieveOperation2 should be complete. Once retrieveOperation2 is completed successfully the formLoad function shall complete. The console should log the statements in this order.

  • operation1 completed successfully
  • printing statement 2
  • operation2 completed successfully.
// form onload method.
function executeOnLoad() {
 // executeOnLoad is the function called on form onload.
   // statement 1
   retrieveOperation1();

   // statement 2 - this should execute after statement 1 is completed.
   console.log("printing statement 2");

   // statement 3
   retrieveOperation2();
}

function retrieveOperation1() {
   Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name")
      .then(function (results) {
         console.log("operation1 completed successfully.");
      }, function (error) {
      });
}

function retrieveOperation2() {
   Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name")
      .then(function (results) {
         console.log("operation2 completed successfully.");

      }, function (error) {
      });
}

And below is the output in the console.

Synchronous Xrm.WebApi calls

Quite obvious isn’t it? Xrm.WebApi functions are promise functions. The code will not wait for the retrieveMultipleRecords operation to complete. It is asynchronous. So statement 2 got printed and after that operation1 and operation2. The output sometimes differ. If operation1 takes more time, then it is even possible that Operation2 statement is printed and then Operation1.

Obviously it is not working as per our expectations. So what can we do about it? Well we have the wonderful async and await feature in Javascript. I wrote a blog on this sometime back but not specifically on Xrm.WebApi.

Below is the modified code to achieve our requirement.

// form onload method.
async function executeOnLoad() {
   // statement 1
   await retrieveOperation1();

   // statement 2 - this should execute after statement 1 is completed.
   console.log("printing statement 2");

   // statement 3
   await retrieveOperation2();

   // statement 4
   console.log("onload event successfully completed.");
}

async function retrieveOperation1() {
   var results = await Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name");
   for (var e = 0; e < results.entities.length; e++) {
      // perform your operations here.
   }

   console.log("retrieve1 operation completed successfully.");
}

async function retrieveOperation2() {
   var results = await Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name");

   for (var e = 0; e < results.entities.length; e++) {
      // perform your operations here.
   }

   console.log("retrieve2 operation completed successfully.");
}

Below are the changes I made

  • Modified the function called on form onload – executeOnLoad to async method.
  • Changed the functions retrieveOperation1 and retrieveOperation2 to async methods.
  • Used the keyword “await” on Xrm.WebApi methods. await can be used with Promise based functions which Xrm.WebApi functions are. Hence the await keyword before them just works fine for us.
  • Just to make things complicated, I have introduced an additional console.log statement post retrieveOperation2 completion.

And now when I run this, I see all the operations executed in order. Wonderful isn’t it.

Synchronous Xrm.WebApi calls.

Observe that the final console.log statement executed post Operation2 completion only. The onload event completion didn’t happen until all the previous steps were completed.

Tested this if Edge/ Edge chromium/ FireFox/ Chrome and it just worked fine. Word of caution here – this won’t work in IE 11

Where you should execute sync and where not – “depends on your requirement”. But this little know tactic certainly is an additional tool in repertoire to implement whenever required.

Hope this helped!

You may also like these posts

Debajit Dutta
(Business Solutions MVP)

Source: Debajit


How to display external webpage inside Dynamics 365/ CDS model driven app
Category: CDS,Dynamics 365,Microsoft CRM Author: debajit prod Date: 3 weeks ago Comments: 0

Do you have the following requirements?

  • Your customer have a web application built on MVC or ASP.NET classic and they want to show up a webpage of that application embedded in your Dynamics 365 App.
  • Your customer have a non .NET web application which they want to show embedded on a Dynamics 365 record on demand

If your requirement is similar to the ones I mentioned, you will have a solution at the end of this blog. And I had exactly the same requirement for my customer where they needed to show up a different application to approve an account on click of a button on the account form.

As you can see from the screenshot below, we have a button – “BING SEARCH” on the account form. When the button is clicked, we will display the BING search with a custom search, embedded on the account. One way is showing the search page as pop-up but pop-up window designs are something out of fashion now.

Xrm.Panel.loadPanel

Turned out it’s actually quite easy and Microsoft infact have an API to do just that. But this API is not explored much. The documentation for the same is right here. While it is neatly documented, the purpose of certainly demands a few more lines.

Xrm.Panel.loadPanel(url, title)

For this example I am going to show on how to display BING search page with customized search query string. I will take the value in Account name field and search with the value. But you can literally show up any page here. Below is the code on button click. Notice how I sent custom search parameter. You can also do the same for your requirement if it necessitates.

// JavaScript source code
function showAccountApprovalPage(fc) {
   var searchString = fc.getAttribute("name").getValue();
   var url = "https://bing.com/search";
   Xrm.Panel.loadPanel(url + "?q=" + searchString, "BING SEARCH");
}

And below is the behavior when I click on ribbon button.

Xrm.Panel.loadPanel

Cool isn’t it? However you should be aware of few things before you use this. All these statements are valid as of the time of writing this blog.

  • The function is only available in WebClient
  • There is no function to hide the panel once the panel is shown.
  • The webpage is shown inside a iframe. So you cannot display any web site which does not allow itself to be loaded inside iframe (like GOOGLE, GMAIL etc.)
  • This can be displayed anywhere on Model driven apps
  • Not supported on mobile devices.

Hope this helps!

You should also like the below posts

Debajit Dutta
(Business Solutions MVP)

Source: Debajit


Upload content to File Attribute in Dynamics 365/ CDS from JavaScript
Category: CDS,Dynamics 365,File attribute content in Dynamics 365,JavaScript,Microsoft CRM,Microsoft Dynamics CRM,Model driven apps Author: Debajit Dutta Date: 3 months ago Comments: 0

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com This has been a long ask from my readers since the time I wrote the blog on how to read contents of File attribute using JavaScript in Dynamics 365. So here I am … Continue reading Upload content to File Attribute in Dynamics 365/ CDS from JavaScript
Source: Debajit


Custom Button on Dashboard not displayed in Dynamics 365 UCI
Category: CDS,Custom button on Dashboard not showing in Dynamics 365,Dynamics 365,Microsoft CRM,Microsoft Dynamics CRM,Model driven apps Author: Debajit Dutta Date: 3 months ago Comments: 0

Originally posted on Helplessheroes:
It is observed that custom ribbon buttons don’t show up in unified interface, to solve this issue follow below steps. Install chrome extension to execute REST calls: • Open Chrome Webstore• Look for extension Yet Another Rest Client and add it to your chrome browser.• Open and login to your Dynamics…
Source: Debajit


Override createdon, modifiedon, createdby, modifiedby in Dynamics 365/ CRM {CRM Tips from the Vault}
Category: CDS,Dynamics 365,Microsoft CRM,Microsoft Dynamics CRM,Model driven apps,override createdby in Dynamics 365,override createdon in Dynamics 365,override modifiedby in Dynamics 365,override modifiedon in Dynamics 365 Author: Debajit Dutta Date: 3 months ago Comments: 0

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com CRM tips from the vault – A series which I have started on request of my blog readers who are quite new to CRM. My blog focus on niche topics which mostly require … Continue reading Override createdon, modifiedon, createdby, modifiedby in Dynamics 365/ CRM {CRM Tips from the Vault}
Source: Debajit


API to get Logged In User’s security role(s) name and ID in Dynamics 365 – Xrm.Utility.getGlobalContext().userSettings.roles. API bug has been fixed. Team role name no longer come as undefined.
Category: CDS,Dynamics 365,Get Security role name of Logged in user in Dynamics 365,Microsoft CRM,Microsoft Dynamics CRM,Model driven apps Author: Debajit Dutta Date: 3 months ago Comments: 0

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com It feels great to write this post. Not because this the first time I am writing this post. But because last time when I wrote a post about this, there existed a bug … Continue reading API to get Logged In User’s security role(s) name and ID in Dynamics 365 – Xrm.Utility.getGlobalContext().userSettings.roles. API bug has been fixed. Team role name no longer come as undefined.
Source: Debajit


Display Notification on Entity Home Page Grid in dynamics 365/ CDS
Category: CDS,Dynamics 365,Microsoft CRM,Microsoft Dynamics CRM,Model driven apps,Notification on Home Page grid in Dynamics 365 Author: Debajit Dutta Date: 4 months ago Comments: 0

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com In this blog I am going to explain on how you can show a notification on Entity Home Page grid. Please follow this blog to where I have been demonstrated to how to … Continue reading Display Notification on Entity Home Page Grid in dynamics 365/ CDS
Source: Debajit


1 2 3 11