With Dynamics CRM 2011 you now have the ability to create multiple forms per entity. This allows you to streamline the data entry process based on roles and fields. Dynamics CRM 2011 already has an out of the box feature to control which form is displayed based on a user’s role. However it is missing the ability to switch the form based on a form value.
So, let’s take a look at how we can change the form to be used based on a form value.
var FormSwitcher = FormSwitcher || {};
FormSwitcher._defaultFormName = "Information";
FormSwitcher.redirectAccountForm = function () {
var formSelector = Xrm.Page.ui.formSelector;
if (formSelector) {
var switchTo = FormSwitcher._defaultFormName;
// do logic here to check which form to use and set the switchTo variable
// once you've figured out the form to switch to, call the internal redirect method to switch the form
FormSwitcher._redirectToForm(switchTo, true);
}
}
// tryDefault prevents infinite loop if default form doesnt exist
FormSwitcher._redirectToForm = function (formName, tryDefault) {
var currentForm = Xrm.Page.ui.formSelector.getCurrentItem();
var availableForms = Xrm.Page.ui.formSelector.items.get();
// if we dont check to see if we're already on the form we'll get into an infinite loop
if (currentForm.getLabel().toLowerCase() != formName.toLowerCase()) {
for (var i in availableForms) {
var form = availableForms[i];
// try to find a form based on the name
if (form.getLabel().toLowerCase() == formName.toLowerCase()) {
form.navigate(); // redirect once we find it
return true;
}
}
}
else {
return false;
}
// nothing found, redirect to default
if (tryDefault && currentForm.getLabel().toLowerCase() != FormSwitcher._defaultFormName) {
return FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);
}
return false;
}
Enjoy!