Sometimes we need to hide or show some fields on the form according to a field value or a condition. We can do it using business rule. but when we want to hide or show large number of fields, tabs, and sections on the form then it will become difficult. In such situation we can directly change form itself where we can add required fields, section, and tab. In this blog, I have explained how to change main form of the entity conditionally using JavaScript.
Here we are going to use JavaScript web resource. As an example we took project entity, where project and project template belong to same project entity and we can differentiate between them using “IsTemplate” field. we are going change main form of project and project template using this field.
var oFormContext = executionContext.getFormContext();
var isTemplate = oFormContext.getAttribute("msdyn_istemplate").getValue();
var Form_Label; if (isTemplate == true) { Form_Label = "Project Template"; } else { Form_Label = "Project"; }
function fnChangeForm(executionContext) { "use strict"; var oFormContext = executionContext.getFormContext(); if (oFormContext != null) { var isTemplate = oFormContext.getAttribute("msdyn_istemplate").getValue(); if (isTemplate != null) { var Form_Label; if (isTemplate == true) { Form_Label = "Project Template"; } else { Form_Label = "Project"; } if (oFormContext.ui.formSelector.getCurrentItem().getLabel() != Form_Label ) { var forms = oFormContext.ui.formSelector.items.get(); for (var i in forms) { var form = forms[i]; var formLabel = form.getLabel() if (formLabel == Form_Label ) { form.navigate(); } } } } } }