Pages

Tuesday, August 2, 2011

CRM 2011 Ribbon Buttons: Use Case scenario

My two previous posts summarized how to go about adding a custom ribbon to a CRM 2011 form. I thought I'd round the topic out by providing a scenario of how these concepts might be applied.

For the sake of this example, let's say that you want to have two buttons on your contact ribbon. One to perform some "Add" logic to add a contact to some list and a second to perform "Remove" logic to remove the contact from the list. The requirements would therefore be as follows:

  • The Add button should execute the "Add" custom function
  • The Remove button should execute the "Remove" custom function
  • The "Add" button should only be enabled when the contact is not already in the list (we could also hide the button but visually it might be more appealing to disable rather than hide the button)
  • The "Remove" button should only be enabled when the contact is already in the list
  • After clicking "Add" to add the contact list, the "Add" button should be immediately disabled and the "Remove" button should be enabled. And vice versa.
  • Both the Add and Remove button should only be displayed on the contact update form and not the contact existing form

In order to achieve the above you would apply the concepts mentioned in the previous 2 posts. Namely:

  1. Add 2 ribbon buttons - One for the Add function and one for the Remove function
  2. The "Add" button should call a custom jscript function that will execute some jscript logic to add the contact to a list
  3. The "Remove" button should call a custom jscript function that will execute some jscript logic to remove the contact from the list
  4. Add EnableRule sections for each button to control when these buttons are enabled/disabled
  5. The "Add" EnableRule section should call a custom jscript function that will perform some logic to determine whether the contact is in the list. It if is it will return false (i.e. disable "add" button as it is already in the list) otherwise it will return true (i.e. enable "add" button as it has not yet been added to the list)
  6. The "Remove" EnableRule section should similarly call a custom jscript function. In fact, in our example as the logic for displaying the "Remove" button is the complete inverse of display the "Add" button (i.e. when "Add" is enabled "Remove" should be disabled and vice versa), we can reference the same custom function as for the "Add" EnableRule and just invert the result (InvertResult="true").
  7. Add a "Xrm.Page.ui.refreshRibbon();" command to the end of custom functions that are responsible for adding/removing the contact from the list. This will ensure that the display rule is executed after the action has been performed which will result in the "Add" and "Remove" buttons being enabled/disabled as necessary.
  8. Add DisplayRule sections for each button to control when these buttons are displayed on the form
  9. In both cases, the DisplayRule section should just have a "<FormStateRule State="Existing"/>" entry which will instruct the buttons to only show on the Update form.

And that should be it!

No comments:

Post a Comment