Friday, October 21, 2016

Refresh Form Ribbon to show / hide button based on attribute value

I had an requirement, when contact has phone number, then show Make a Call custom button. 
To fulfill this requirement, I created custom button and added action and enable rule to custom button. 

In my button enable rule I am checking for Phone number attribute, if it has value then show button other wise hide button. My JavaScript function is

whenToShowMakeACall: function () {
        var phoneNo = Xrm.Page.getAttribute('telephone1').getValue();
        if (phoneNo != null)
            return true;
        else
            return false;

    },

This is working fine, when contact record has phone number in record and when that record opened button was shown.
But now requirement changed, customer wants to show button, as soon as Phone number entered.
If want to show button as soon as phone number entered, means I need to either save contact record or need to refresh ribbon. 

I cannot save contact record because, there might be some other required attributes are missing. 

Now only option is to refresh ribbon. Till CRM 2016, there is not any supported method to refresh ribbon.  In MS CRM 2016, now we have JavaScript method to refresh ribbon, This method solved my problem. 

I created one JavaScript method and calling on phone number OnChange event to refresh ribbon. 

refreshRibbonNavigation: function () {
        Xrm.Page.ui.refreshRibbon();

    },


No Phone number - No Make a Call button 


When phone number, showing Make a Call button


With Xrm.Page.ui.refreshRibbon();  method, you can show / hide any button based on attribute value.








4 comments:

  1. thanks for the post it is useful

    ReplyDelete
  2. Perfect. Thank you. Just what I was looking for.

    ReplyDelete
  3. whenToShowMakeACall, this function will be on Form Load Or Form OnSave Event.

    ReplyDelete
  4. THANK YOU FOR THE INFORMATION
    PLEASE VISIT US
    CRM Solutions



    ReplyDelete