Tuesday, April 28, 2015

Change Customer Lookup Behaviour

In MS CRM there are some lookups which allows multiple entities, like Customer lookup. 
When clicked on customer lookup, we have choice to select either account or contact.





But some time requirements are allow either account or contact only for customer lookup or default customer lookup to contact instead of account.
Right now there is not any supported way to achieve this, but with unsupported way you can easily achieve these requirements.

Note: Below code is unsupported code for MS CRM. JavaScript code is using DOM.

Add following JavaScript code on form load.

1.     Customer lookup default to Contact.


function CustomerLookupDefaultToContact() {
    var customerCtrl = Xrm.Page.getControl("customerid");
    customerCtrl.setFocus();

    var customerInput = document.getElementById("customerid_i");
    customerInput.setAttribute("lookuptypes", "2,1");   //2: Contact , 1: Account
    customerInput.setAttribute("defaulttype", 2);       // Set defaultType to Contact
    customerInput.setAttribute("autoresolve", 2);       // Partially entered name will be resolved with contact record.
    customerInput.setAttribute("createpermissiondictionary", "account:true,contact:true");
    customerInput.setAttribute("lookuptypenames", "account:1:Account,contact:2:Contact");
    customerInput.setAttribute("DefaultViewId", "{A2D479C5-53E3-4C69-ADDD-802327E67A0D}"); // View id to set Default contact view. Replace with your View Id

}





2.     Customer lookup only for Contacts


function CustomerLookupOnlyContact() {
  
    var customerCtrl = Xrm.Page.getControl("customerid");
    customerCtrl.setFocus();

    var customerInput = document.getElementById("customerid_i");
    customerInput.setAttribute("lookuptypes", "2");   //2: Contact , 1: Account
    customerInput.setAttribute("defaulttype", 2);       // Set defaultType to Contact
    customerInput.setAttribute("autoresolve", 2);       // Partially entered name will be resolved with contact record.
    customerInput.setAttribute("createpermissiondictionary", "account:false,contact:true");
    customerInput.setAttribute("lookuptypenames", "contact:2:Contact");
    customerInput.setAttribute("DefaultViewId", "{A2D479C5-53E3-4C69-ADDD-802327E67A0D}"); // View id to set Default contact view. Replace with your View Id

}



3.     Customer lookup only for Account

function CustomerLookupOnlyAccount() {
  
    var customerCtrl = Xrm.Page.getControl("customerid");
    customerCtrl.setFocus();

    var customerInput = document.getElementById("customerid_i");
    customerInput.setAttribute("lookuptypes", "1");   //2: Contact , 1: Account
    customerInput.setAttribute("defaulttype", 1);       // Set defaultType to Contact
    customerInput.setAttribute("autoresolve", 1);       // Partially entered name will be resolved with contact record.
    customerInput.setAttribute("createpermissiondictionary", "account:false,contact:true");
    customerInput.setAttribute("lookuptypenames", "account:1:Account");
    customerInput.setAttribute("DefaultViewId", "{A9AF0AB8-861D-4CFA-92A5-C6281FED7FAB}"); // Default view to show records. Replace with your View Id

}




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;
        }
        else
            return true;
    }
For CRM 2015
function HideButtonBasedOnViewSelected (selectedCtrl) {             
        var query = selectedCtrl.get_viewTitle();
        if (query == 'Your View Name') {
            return false;
        }
        else
            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;
        }
        else
            return true;
    }

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

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


Tuesday, April 7, 2015

Show all text value for CRM form header attribute

In CRM you can display attributes into form Header, but sometime attribute contains large text, and that text is not properly shown on form header. Part of the text is shaded, and user is not able to see complete value until user mouse hover on that field. 



To show complete text or attribute value you need to change form header formatting.
Default CRM header formatting is three columns.


In my form header I have two attributes and one has large text, so changed formatting for that large text attribute from one column to two columns.

To change formatting for attribute, select attribute and click on change properties button shown on form ribbon.

In Field property window, click on Formatting tab, and select two columns formatting.


Once changed formatting for attribute, rearrange the header attributes.
Save and publish your form and customization. 
Now header is showing complete text / value of attribute


Note: Once you change formatting of header, attributes are become read-only. It is look like a bug in CRM application.
If you want you can vote for this bug on 
So that Microsoft will try to resolve this issue in future release.