Wednesday, January 18, 2012

Enabling and Disabling custom Buttons in CRM 2011

There is a lot of posts out there explaining this my 2 cents to clear out more doubts.
I have custom buttons(forward and return)  in my form which I need to enable and disable according to the logged in user profile and also cheking the status.
There are basically some steps involved in this.

1.First create a new solution and add the entity you are trying to disable or enable the buttons.
2.Export the solution zip file and extract the files.
3.The custom.xml has to be modified.
4.First search for you entity in the custom xml
5.In Command Definition  enable the Rule ID, here I am enabling the “Forward” and “Return” custom Buttons.
CommandDefinition Id="Mscrm.Isv.fas_travelbudget.Form.Group0.Control1">
  <EnableRules />
  <DisplayRules />
- <Actions>
  <JavaScriptFunction FunctionName="Mscrm_Isv_fas_travelbudget_Form_Group0_Control1_2" Library="$Webresource:fas_travelbudget_ribbon.js" />
  Actions>
  CommandDefinition>

- <CommandDefinition Id="Mscrm.Isv.fas_travelbudget.Form.Group0.Control2">
- <EnableRules>
  <EnableRule Id="Mscrm.Isv.fas_travelbudget.Form.Group0.Control2.EnableRule" />
  EnableRules>
  <DisplayRules />
- <Actions>
  <JavaScriptFunction FunctionName="Mscrm_Isv_fas_travelbudget_Form_Group0_Control2_3" Library="$Webresource:fas_travelbudget_ribbon.js" />
  Actions>
  CommandDefinition>

-
6.In Rule Definition include the Function name “enableForwardButton” and “enableReturnButton” and use the appropriate  web resource.

- &
  DisplayRules>
- <EnableRules>
- <EnableRule Id="Mscrm.Isv.fas_travelbudget.Form.Group0.Control0.EnableRule">
  <CustomRule FunctionName="enableForwardButton" Library="$Webresource:fas_travelbudget_main_library.js" />
  EnableRule>
- <EnableRule Id="Mscrm.Isv.fas_travelbudget.Form.Group0.Control2.EnableRule">
  <CustomRule FunctionName="enableReturnButton" Library="$Webresource:fas_travelbudget_main_library.js" />
  EnableRule>
  EnableRules>
  RuleDefinitions>
7.In Web resource(Library="$Webresource:fas_travelbudget_main_library.js" ) add the following, here we check the status code and also check one of the lookup fields to see if it matches with the "user id".

var forwardButton;
var returnButton;
forwardButton = false;
returnButton = false;


if ((supervisorid == whoamiUserId)  && (Xrm.Page.getAttribute("statuscode").getValue() == 1))
{
      forwardButton = false;
}

if ((supervisorid == whoamiUserId) && (Xrm.Page.getAttribute("statuscode").getValue() == "7"))
{
returnButton = false;
}

8.The zip the modified xml and import it in the solution.