Showing posts with label ribbon button. Show all posts
Showing posts with label ribbon button. 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;
            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 () {


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.

Friday, May 13, 2016

Inject your code before CRM button event - Conditional deactivation of CRM record

I came across business requirement like, if there are any active child records, then show error message to users when deactivating parent record.

I can use plugin or real time workflow for this, but when error message thrown, CRM throws error like Business process error.

But customer doesn’t want to see this business process error, customer wants Alert message, and do not want to deactivate record.
To full fill this requirement I customized CRM button command, and added my own script on deactivate button.
Here are steps I did

I am using Ribbon workbench to customize entity ribbon.

1.       Customize Deactivate command

2.       Once command is customized you will see JavaScript library and function used by MS CRM.

3.  Change function Name to NaN and remove parameters for CRM default deactivates action.

This we need to do so when condition is satisfied, record should be deactivated and CRM JavaScript library should be loaded. 

4.       Now add your own JavaScript action in deactivate command, and call your own JavaScript function
When calling own JavaScript function make sure you are using same Parameters used in CRM default deactivates function, as we are going to call this function in our own JavaScript.

5.       In your JavaScript code, you can check, is there any active child records associated with this record? If not then deactivate record, otherwise show message. 

JavaScript code is

CheckOnDeactivation: function (CompanyId, entityName) {
        var req = new XMLHttpRequest();"GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/contact?$select=contactId&$filter=parentcustomerid/Id eq (guid'" + CompanyId + "') and statecode/Value eq 0", false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                this.onreadystatechange = null;
                if (this.status === 200) {
                    var returned = JSON.parse(this.responseText).d;
                    var results = returned.results;
                    if (results.length >= 1) {
                        alert('You cannot deactivate this record because it has active child records.');                        
                        return false;
                    else {
                        Mscrm.CommandBarActions.deactivate(CompanyId, entityName);
                        return true;
                else {

6. Save and Publish customization
Now when trying to deactivate parent if it has any active child then error will be thrown and record will not be deactivated. 

You can use same technique for any other button command.  

Tuesday, May 19, 2015

Hide / Show Export to Excel button for specific Entity / Specific View

In MS CRM, Export to excel feature is enabled or disabled from security role.
You can have this feature for all applicable entities or none of the entity.
But sometime we have requirement to hide export to excel from particular entity, or show only for particular entity.
With Security role this requirement is not possible, but will little JavaScript code and ribbon customization we can achieve this.

Case 1: Hide Export to Excel button from contact Home grid.

1.      Create one custom solution
2.      Add Application Ribbons – from client Extensions

3.      Create one JavaScript web resource in this solution and add following code

function ShowHideExport2Excel(SelectedEntityTypeName) {
    if (SelectedEntityTypeName == 'contact')
        return false// hide button
        return true//show button

4.       Now Open this custom solution into Ribbon workbench
5.      In the Entities list you will see ApplicationRibbon
6.      Select Export to Excel button from Home, right click and Customize command

7.      In Commands, you will see Mscrm.ExportToExcel
8.      Now add custom Enable Rule to this command. Keep existing enable rules.
9.      When adding enable rule, add Custom JavaScript rule.
Default: True
FunctionName: ShowHideExport2Excel
invertResult: False
Library: your JavaScript library
Parameters :  [Crm Parameter] = SelectedEntityTypeName

10.  Add Crm Parameter to Custom JavaScript rule, value of this parameter must be SelectedEntityTypeName

11.   Save and publish your customization.
12.  Now Export to Excel button is hidden for Contact home grid.

Export To Excel button is not showing now.

 Export To Excel button is showing now.

Case 2: Show Export to Excel button only for contact Home grid.

In this case you just need to change JavaScript code little bit as

function ShowHideExport2Excel(SelectedEntityTypeName) {
    if (SelectedEntityTypeName == 'contact')
        return true// show button
        return false//hide button

Case 3: Show Export to excel button for particular entity and particular view only

[To do this need to use Unsupported JavaScript code]
To do this, we need to add one more Crm parameter to enable rule, and select value to this parameter is SelectedControl

And modify JavaScript function as

For MS CRM 2013 :

function ShowHideExport2Excel(SelectedEntityTypeName, selectedCtrl) {
    if (SelectedEntityTypeName == 'contact') {
        var view = selectedCtrl.get_$1X_3();
        var viewName = view.selectedViewName;
        if (viewName == 'My Active Contacts') //Change "My Active Contacts" with Your view name to show Export To Excel button
            return true; // show button
            return false; // hide button
        return true;

For MS CRM 2015:

function ShowHideExport2Excel(SelectedEntityTypeName, selectedCtrl) {
    if (SelectedEntityTypeName == 'contact') {        
        var viewName = selectedCtrl.get_viewTitle();
        if (viewName == 'My Active Contacts'//Change "My Active Contacts" with Your view name to show Export To Excel button
            return true// show button
            return false// hide button
        return true;

Wednesday, May 6, 2015

Running CRM Dialog from Custom Ribbon button

 1.      Add custom button on form
Say Run Dialog

     2. Now create command for custom button

     3. Add  Action to command

For the action add “Open Url Action”

3. For Url Command set your dialog URL
         To get dialog URL run dialog using “Start Dialog” button

Then select dialog you want to run on button click, once dialog started copy URL of dialog from address bar

 5. Your dialog url will be look like 

https://{CRM Server}:444/cs/dialog/rundialog.aspx?DialogId=%7b83D90936-EBEC-4CE5-8A58-A1530DE30680%7d&EntityName=new_test&ObjectId=%7b0FCB7F66-5FA6-E411-B1DF-0050568C6D7D%7d

It has Dialogid, Entityname and ObjectId
ObjectId is the record id on which this Dialog is running.

 6. If you use this URL as it is on button command then dialog will be run on same record again and again. 

      7.  To pass ObjectId / record id dynamically, need to pass one parameter to Url command, defined for custom button. This parameter should be like

    8. Now update your URL command address to

https://{CRM Server}:444/cs/dialog/rundialog.aspx?DialogId=%7b83D90936-EBEC-4CE5-8A58-A1530DE30680%7d&EntityName=new_test

Noticed that I removed &ObjectId=%7b0FCB7F66-5FA6-E411-B1DF-0050568C6D7D%7d, this objectId is passed by parameter, that’s why Parameter Name is important. 

    9.  Save and publish your customization.

   10.   Now dialog will be run for record you are in from button.

Monday, April 27, 2015

Hide Ribbon Button based on View selected

Sometimes we need to hide some button based on view selected.
CRM doesn’t have this functionality, and there is not supported way to achieve this. With small unsupported JavaScript code you can easily hide ribbon buttons based on view selected.
To do this, need to add Enable rule to button command.
I am using Ribbon workbench to add Enable Rule.
1.      If want to hide custom button then select button command, and click on Enable rules

If want to hide existing CRM button then need to customize button command, and then add Enable rule.

2.      Add new Enable rule to command

3.      Add Step to Enable rule and select Custom JavaScript Rule

4.      Set properties to javascript rule as

Default : True
FunctionName: HideButtonBasedOnViewSelected
InvertResult: False
Library: select JavaScript library where HideButtonBasedOnViewSelected function code is.
Parameters: Need one parameter to HideButtonBasedOnViewSelected function,

Add Crm parameter, and set parameter value as SelectedControl

5.      Now save and upload changes to CRM, and publish changes.
6.      JavaScript code required to hide button is
for CRM 2013
function HideButtonBasedOnViewSelected (selectedCtrl) {
        var view = selectedCtrl.get_$1X_3();
        var query = view.selectedViewName;

        if (query == 'Your View Name') {
            return false;
            return true;
For CRM 2015
function HideButtonBasedOnViewSelected (selectedCtrl) {             
        var query = selectedCtrl.get_viewTitle();
        if (query == 'Your View Name') {
            return false;
            return true;
To get selected view name need to use some unsupported JavaScript. 
"selectedCtrl.get_$1X_3();" or "selectedCtrl.get_viewTitle()"
This is giving all details about selected view and default view of this grid. 
Here I am hiding button for all views except view name is "Active Students - 2015"
For CRM 2013:
function HideButtonBasedOnViewSelected(selectedCtrl) {
        var view = selectedCtrl.get_$1X_3();
        var query = view.selectedViewName;

        if (query != 'Active Students - 2015') {
            return false;
            return true;

For CRM 2015:
function HideButtonBasedOnViewSelected(selectedCtrl) {        
           var query = selectedCtrl.get_viewTitle();
        if (query != 'Active Students - 2015') {
            return false;
            return true;

Enroll button is visible only for Active student -2015 view.