In the previous recipes of this chapter we have seen some examples on working with the form UI based on either a predefined rule on form load, or on a field's onchange
event. This example will focus on putting it all together in order to achieve a comprehensive result that actually can satisfy a realistic business rule.
Let's go back to the solution that we have worked with in the previous recipe. We will be using the same Contact form to implement this example. Following the Form load event usage recipe described earlier in this chapter, we can easily realize that it only works first time when the user loads a contact. How about if the user changes a value on the form? We have touched on the functionality a little bit in the following recipes, but let's put it all together now.
The following is what we want to achieve:
Up until now, the first two conditions are met by the recipe described earlier. Now let's focus on the third requirements.
function ChangeCustomer() { var _isSpecialSelection = null; var _isSpecial = Xrm.Page.getAttribute("new_isspecialcustomer"); if(_isSpecial != null) { _isSpecialSelection = _isSpecial.getValue(); } if(_isSpecialSelection == false) { // hide the Special Customer tab Xrm.Page.ui.tabs.get("tab_5").setVisible(false); // hide the Customer Classification field Xrm.Page.ui.controls.get("new_customerclassification").setVisible(false); // hide the Partner field Xrm.Page.ui.controls.get("new_partner").setVisible(false); } else if(_isSpecialSelection == true) { // show the Special Customer tab Xrm.Page.ui.tabs.get("tab_5").setVisible(true); // show the Customer Classification field Xrm.Page.ui.controls.get("new_customerclassification").setVisible(true); // show the Partner field Xrm.Page.ui.controls.get("new_partner").setVisible(true); } }
Taking a closer look at the function presented earlier, there are really a handful of actions we take.
True
/False
value. We set this to null
, note that if it does not get assigned we can skip execution of any other code:var _isSpecialSelection = null;
var _isSpecial = Xrm.Page.getAttribute("new_isspecialcustomer");
if(_isSpecial != null) { _isSpecialSelection = _isSpecial.getValue(); }
false
, and then we hide the Form tab and fields:if(_isSpecialSelection == false) { // hide the Special Customer tab Xrm.Page.ui.tabs.get("tab_5").setVisible(false); // hide the Customer Classification field Xrm.Page.ui.controls.get("new_customerclassification").setVisible(false); // hide the Partner field Xrm.Page.ui.controls.get("new_partner").setVisible(false); }
true
so we can show these elements back to the user:else if(_isSpecialSelection == true) { // show the Special Customer tab Xrm.Page.ui.tabs.get("tab_5").setVisible(true); // show the Customer Classification field Xrm.Page.ui.controls.get("new_customerclassification").setVisible(true); // show the Partner field Xrm.Page.ui.controls.get("new_partner").setVisible(true); }
If the value is not assigned, and it remains null as we defined it at the beginning, we do not execute any code. If there is a requirement to execute an action for that case, you can easily add another else if
block at the end.
A few things we have to be aware of when designing our forms include the following aspects:
Working with form elements in Dynamics CRM assumes that all these fields are precreated and added to the forms. You cannot dynamically create new form fields and add them.
While this might not catch your attention right away, depending on where you drop your field on the form, after a while you will observe that when you hide a field, the remaining fields underneath do not rearrange automatically. This is because, while you have your field hidden, it still exists on the page at that specified location.
When designing your page, arrange your individual fields that you will hide in such a way so that when you hide one, it does not leave an obvious gap on the form. Either place them at the bottom of a section or tab, or place them in such an order that while you start showing them, they get added at the bottom of the previous one. This works in a case where you have a business progression expected, and you display items on the form as you progress through phases predefined.
3.129.73.142