ttajic
10/22/2015 - 1:40 PM

Switch form in javascript From http://www.magnetismsolutions.com/blog/gayanperera/2013/07/24/how-to-switch-a-dynamics-crm-form-based-on-a-va

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;
}