Ribbon debug/Command checker tool

Image
Background: Usually when something goes wrong in the button visibility or to find out the rules, actions(js) being used in the ribbon button, we go and find either in the ribbondiffxml or through ribbon workbench. This is a time taking process.  Solution: Now there is a built-in tool called 'Command Checker' available. Using this tool, we can easily troubleshoot the button visibility related issues in home page grid, entity form, sub grid and global command bar as well.  To use/enable this tool, pass "ribbondebug=true" parameter in the url. Example, if you want to troubleshoot some button in Account form, below is the url,  https://trial.crm5.dynamics.com/main.aspx?appid=4db5bcb8-675b-ec11-8f8f-002248599a15&pagetype=entityrecord&etn=account&id=83883308-7ad5-ea11-a813-000d3a33f3b4 &ribbondebug=true This will enable the command bar in 3 places. 1. In the header's right side 2. In the Form command bar 3. If the form has any sub grids. Co...

Run async calls sequentially in D365 JS

With the introduction of "Xrm.WebApi.online", calls to CRM api becomes asynchronous always. Due to this, most of the times, we need to use ".then()" function and write the logic which needs to be executed next, or we are going with "XMLHttpRequest" for making synchronous calls. 

Now, using 'async and await' keywords, we can make the code to run in a synchronous way with "Xrm.WebApi.online" as well.

Example:

Consider the following requirement.

In Contact home page, show or hide the 'Summary' ribbon button based on the view selected. the view name is stored in the custom entity 'Configuration Settings' which has two fields: Name and Value.

There is already a method implemented to retrieve the Configuration Settings Value based on the Name passed using Xrm.WebApi.online.retrieveMultipleRecords.

 function GetConfigSettings(viewName) {  
   try {  
     var result = Xrm.WebApi.online.retrieveMultipleRecords("new_configsetting", "?$select=new_value&$filter=new_name eq '" + viewName + "'").then(  
       function success(results) {  
         if (results.entities.length > 0) {  
           return results.entities[0]["new_value"];  
         }  
       },  
       function (error) {  
         return error.message;  
       }  
     );  
     return result;  
   }  
   catch (e) {  
     return e.message;  
   }  
 };  

So, we need to consume this method and get the view name synchronously to show or hide the summary button.

Below is the custom js called from Summary button enable rule.

 function showhideSummary(selectedControl) {  
   var enable = false;  
   try {  
     if (selectedControl.getEntityName() === "contact"  
       && selectedControl.getViewSelector().getCurrentView() !== null) {  
       var viewName = GetConfigSettings("Active Contacts");  
       if (selectedControl.getViewSelector().getCurrentView().name === viewName) {  
         enable = true;  
       }  
       else {  
         enable = false;  
       }  
     }  
     else {  
       enable = false;  
     }  
   }  
   catch (e) {  
     Xrm.Navigation.openAlertDialog(e.message);  
   }  
   return enable;  
 };  
Since Xrm.WebApi.online.retrieveMultipleRecords returns the result asynchronously, we will always get 'undefined' in the viewName variable in showhideSummary function and the Summary button will always be hidden. 

So, to make this call synchronous, 
  • async keyword should be used before the function which returns the result asynchronously or calls the asynchronous function 
  • await keyword should be used when making the call to asynchronous functions.
The functions should be implemented as below.
 async function GetConfigSettings(viewName) {  
   try {  
     var result = await Xrm.WebApi.online.retrieveMultipleRecords("new_configsetting", "?$select=new_value&$filter=new_name eq '" + viewName + "'").then(  
       function success(results) {  
         if (results.entities.length > 0) {  
           return results.entities[0]["new_value"];  
         }  
       },  
       function (error) {  
         return error.message;  
       }  
     );  
     return result;  
   }  
   catch (e) {  
     return e.message;  
   }  
 };  
   
 async function showhideSummary(selectedControl) {  
   var enable = false;  
   try {  
     if (selectedControl.getEntityName() === "contact"  
       && selectedControl.getViewSelector().getCurrentView() !== null) {  
       var viewName = await GetConfigSettings("Active Contacts");  
       if (selectedControl.getViewSelector().getCurrentView().name === viewName) {  
         enable = true;  
       }  
       else {  
         enable = false;  
       }  
     }  
     else {  
       enable = false;  
     }  
   }  
   catch (e) {  
     Xrm.Navigation.openAlertDialog(e.message);  
   }  
   return enable;  
 };  

Comments

Popular posts from this blog

Convert datetime to user local in D365 Plugins

Show/Hide ribbon button based on the selected view in the D365 subgrid

Ribbon debug/Command checker tool