Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

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.








Wednesday, March 25, 2015

Access Quick View Form values in JavaScript

Generally if want to access any control or attribute in CRM form script you will use control or attribute name.. For Quick form it is little bit complicated.

If I have Quick form for custom entity new_manufacture, and I added this quick form in new_car entity form.

To access new_manufacturer quick form in car entity form I need to use Quick form name, entity name and attribute name to get value.

To access in JavaScript Quick view, control name should be like
QuickFormname_QuickFormname_EntityName_AttributeName



My Quick Form name is : manufactureQuickView
Entity Name : new_manufacture
Attribute: new_Country

if (Xrm.Page.getControl('manufactureQuickView_manufactureQuickView_new_manufacture_new_Country') != null)
{
            var manfCountyctrl = Xrm.Page.getControl('manufactureQuickView_manufactureQuickView_new_manufacture_new_Country');
            var country = manfCountyctrl.getAttribute().getValue();


        }