Blog, Development

Showing ribbon button based on the result of async operation

Earlier when I had a task to show/hide button based on the result of data-retrieval operation I used following approach – declared variable, made synchronous call to endpoint, set that variable inside the callback and returned true/false result at the end of validation function. It worked but if the operation was long-running it caused blocking of UI (that is bad and not user-friendly at all). In this post I’ll show how to work around this situation.

Example I will use today is classic – when user doesn’t have specific role (System Administrator in my case) button is not available for user. Also I developed and tested everything on v9.0 so it could require change on your environment if its of earlier version.

1. Please check code with comments:

2. Ribbon Configuration – there are not a lot of special things to mention but to make it compatible with UCI formContext should be passed inside “Custom Enable Rule”:

Detailed description how to work with formContext in UCI-compatible code you can find here.

UPD approach mentioned will work for buttons that are located on the form. If you want to refresh ribbon of grid check this post.

12 Comments on “Showing ribbon button based on the result of async operation

  1. Hi Andrew

    It works fine! But, if the button is inside a dropdown list, the enable rule code is executed when you click on the arrow and you have to click again to make it appear or hide the button, would there be any way for the enable rule to be executed when you load the form ?

    Thank you

    1. Alberto,
      That’s an interesting usecase. To be honest I haven’t tried that so I don’t have an answer ATM.
      Thanks,
      Andrew

  2. In my case I had a problem because the results are fetched only once. So for dynamically changing values I had to modify it. In my version, I left out the ‘isAsyncOperationCompleted’ part and instead, compared the flag result of the query with the current one, update it if necessary and refresh the ribbon if they’re different. That way, I avoid infinitely refreshing the ribbon while keeping the value updated.

    1. Kevin,
      I agree. My scenario is applicable when you don’t have dynamically changing values. Security roles is not the thing that keep changing every minute 🙂
      Thanks for comment and interest to my post.
      Andrew

  3. Has anyone had issues with this technique after updating to CRM 9.0? We are using this in a CommandBar button (aka subGrid ribbon) and are finding refreshRibbon(true) — which is documented to refresh all buttons on all command bars — does not refresh our button.

      1. Hi Andrew,

        I have followed the steps mentioned but we are stuck with a problem that after save the functionality to show button is firing up and eventually the isAsyncoperation flag is stopping the actual functionality once it is set. How to overcome this scenario?

        1. Manish,
          I haven’t checked that scenario but generally speaking try to replace following lines:
          if (isAsyncOperationCompleted) {
          return isButtonEnabled;
          }
          with following:
          if (isAsyncOperationCompleted) {
          isAsyncOperationCompleted = false;
          return isButtonEnabled;
          }

          This technique should help you.

          Andrew

        2. Thanks Both. I think I am in agreement with what you have suggested. @Andrew I realized after posting that flag should be made false. Thanks for updating..

        3. Try following my suggestion in this comment https://butenko.pro/2018/11/13/showing-ribbon-button-based-on-the-result-of-async-operation/#comment-8109 It should work for frequently updating values by throwing out the check for isAsyncOperationCompleted and replace it with the result. When the async operation is finished, if the result is different than the flag, the flag will be updated and the ribbon will refresh one time until another refresh is manually triggered from somewhere else

  4. Hi Andrew ,

    On the same note after having implemented this functionality the command is not working even though on click enable rule is firing up first and then command not getting triggered. This looks to be unrelated though but is it something that you have faced as well on button click?

    Thanks

    1. Manish,
      It’s hard to give you a good answer in this case unfortunately without checking the code. When I experience such issues and browser doesn’t show any errors I usually removing commands/configurations to moment when button works again and analyze which of removed commands/settings were able to cause an error.
      Thanks,
      Andrew

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.