In the Creating a rating gauge field recipe of Chapter 7, Extended UI Manipulation, we described a simple method to place a rating gauge on a form and changing the image based on a business rule. Similar to that, this time we will look at placing that rating gauge in the ribbon. The advantage to putting this kind of information on the ribbon is that it's always visible to the user no matter what section of the form you scroll to. So let's see how we can achieve this.
For this example we can use either an existing solution, or we can create a new one. Make sure you have permissions to make customizations to the system. We will add this to the Account entity.
Perform the following steps to add this functionality:
function RibbonPlaceholder() { // empty placeholder }
customizations.xml
file for editing.RibbonDiffXml
area within.<CustomActions> <CustomAction Id="OC.account.Form.Red.CustomAction" Location="Mscrm.Form.account.MainTab.Actions.Controls._children" Sequence="33"> <CommandUIDefinition> <Button Id="OC.account.form.Red.Button" Command="OC.account.form.Red.Command" LabelText=" " ToolTipTitle=" " ToolTipDescription=" " TemplateAlias="o3" Image16by16="$webresource:new_red16" Image32by32="$webresource:new_red32" /> </CommandUIDefinition> </CustomAction> <CustomAction Id="OC.account.Form.Yellow.CustomAction" Location="Mscrm.Form.account.MainTab.Actions.Controls._children" Sequence="34"> <CommandUIDefinition> <Button Id="OC.account.form.Yellow.Button" Command="OC.account.form.Yellow.Command" LabelText=" " ToolTipTitle=" " ToolTipDescription=" " TemplateAlias="o3" Image16by16="$webresourcenew_yellow16" Image32by32="$webresource:new_yellow32" /> </CommandUIDefinition> </CustomAction> <CustomAction Id="OC.account.Form.Green.CustomAction" Location="Mscrm.Form.account.MainTab.Actions.Controls._children" Sequence="35"> <CommandUIDefinition> <Button Id="OC.account.form.Green.Button" Command="OC.account.form.Green.Command" LabelText=" " ToolTipTitle=" " ToolTipDescription=" " TemplateAlias="o3" Image16by16="$webresource:new_green16" Image32by32="$webresource:new_green32" /> </CommandUIDefinition> </CustomAction> </CustomActions>
<CommandDefinition Id="OC.account.Form.Red.Command"> <EnableRules></EnableRules> <DisplayRules></DisplayRules> <Actions> <JavaScriptFunction Library="$webresource:new_ribbongauge" FunctionName="RibbonPlaceholder" /> </Actions> </CommandDefinition> <CommandDefinition Id="OC.account.Form.Yellow.Command"> <EnableRules></EnableRules> <DisplayRules></DisplayRules> <Actions> <JavaScriptFunction Library="$webresource:new_ribbongauge" FunctionName="RibbonPlaceholder" /> </Actions> </CommandDefinition> <CommandDefinition Id="OC.account.Form.Green.Command"> <EnableRules></EnableRules> <DisplayRules></DisplayRules> <Actions> <JavaScriptFunction Library="$webresource:new_ribbongauge" FunctionName="RibbonPlaceholder" /> </Actions> </CommandDefinition>
new_ratinggauge
. Add three values to it, Red
, Yellow
, and Green
.new_jsratinggauge
. Set it of type Script.function jsratinggauge() { var _fieldValue = ""; try { var _fieldValue = Xrm.Page.getAttribute("new_ratinggauge").getSelectedOption().text; } catch(err) { } if(_fieldValue == "Red") { HideButtons("Red"); } else if(_fieldValue == "Yellow") { HideButtons("Yellow"); } else if(_fieldValue == "Green") { HideButtons("Green"); } else { HideButtons("None"); } }
function HideButtons(validButton) { if(validButton == "Red") { var btnRed = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Red.Button-Large"); btnRed.style.display = 'inline'; var btnYellow = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Yellow.Button-Large"); btnYellow.style.display = 'none'; var btnGreen = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Green.Button-Large"); btnGreen.style.display = 'none'; } else if(validButton == "Yellow") { var btnRed = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Red.Button-Large"); btnRed.style.display = 'none'; var btnYellow = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Yellow.Button-Large"); btnYellow.style.display = 'inline'; var btnGreen = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Green.Button-Large"); btnGreen.style.display = 'none'; } else if(validButton == "Green") var btnRed = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Red.Button-Large"); btnRed.style.display = 'none'; var btnYellow = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Yellow.Button-Large"); btnYellow.style.display = 'none'; var btnGreen = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Green.Button-Large"); btnGreen.style.display = 'inline'; } else { var btnRed = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Red.Button-Large"); btnRed.style.display = 'none'; var btnYellow = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Yellow.Button-Large"); btnYellow.style.display = 'none'; var btnGreen = top.document.getElementById("account|NoRelationship|Form|OC.account.form.Green.Button-Large"); btnGreen.style.display = 'none'; } }
jsratinggauge()
function to the OnChange
event of the option set we created earlier. This will cause the ribbon to update dynamically once the selection changes.jsratinggauge()
function to the form's OnLoad
event. This way, when we open a new record, the gauge updates.The approach of this solution is similar to the way we customize a form. We add to the ribbon all the elements we use, and then, based on a specified value or business rule result, we hide or show specific ribbon buttons.
The reason I have split the execution onto two functions is because the first function called is using the CRM API, while the second function called from the previous one is done using the HTML DOM.
18.224.51.55