Pages

Thursday, May 21, 2015

Dynamic CRM Hide/Show ribbon button by OnChange lookup field

1)create a javascript (HideShow.js), 2 functions

var HideShowButton = {
//refresh the ribbon when lookup field on change.
OnChangeLookup: function () {
        Xrm.Page.ui.refreshRibbon();
  },

//Show and Hide when form load
EnableCustomRule: function () {
if (Xrm.Page.data.entity.attributes.get("contactid") != null) {
   var contactId = Xrm.Page.data.entity.attributes.get("contactid").getValue()[0].id;
   var req = new XMLHttpRequest();
   var url = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet(guid'" + contactId + "')?$select=photoonfile";
   req.open("GET", encodeURI(url), false);
   req.setRequestHeader("Accept", "application/json");
   req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
   req.send();
   var data = JSON.parse(req.responseText).d;
   if (data.photoonfile == null || data.photoonfile == false) {
return true;
   }
   return false;
}
},
};


2)Open ribbon workbench editor, add new button (PHOTO ON FILE), command(PhotoonFile.Command), enable custom rule (PhotoonFile.EnableRule). See the image below.


3)Goto form, select the lookup field, add events (HideShow.js->HideShowButton.OnChangeLookup), see the image below.


4)Publish.

Test case 1
if photo on file (photoonfile) is true, "PHOTO ON FILE" button will be hide.
if photo on file (photoonfile) is false, "PHOTO ON FILE" button will be show.

Test case 2, if user selects different lookup record, OnChangeLookup function will call, the page refresh the ribbon button and next Test case 1 will be execute.

No comments:

Post a Comment