Hello,


I have read through a few previously forum questions and cannot quite find the exact answer I am looking for. I have a JS code to change the form based on a selected option set. I have this set OnLoad and OnSave events.

It is an Informal Case form and Formal Case form (with corresponding values in the "Case Type" field, to trigger the form change)

The code works, but I would like a little added flexibility. It will currently only work if the field is selected prior to loading, or if I change the field value and save the form. Is there a way I can have the form automatically save and refresh as the value is changed. If someone creates a new case from scratch, the "Case Type" field is blank, I would like the form to automatically change as soon as the Case Type field value is selected (not waiting for it to be done after saving).

Is there a way to do this? Can I add a piece of code to automatically save an update the form once the case value is selected? Is it possible with an OnChange event code.

My code:

var FormSwitcher = FormSwitcher || {};
FormSwitcher._defaultFormName = "Informal Case";

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

function FormOnLoad()
{
    var casetype_INFORMAL = 100000000;
    var casetype_FORMAL = 100000001;
    var casetype = Xrm.Page.getAttribute('new_casetype').getValue();

    switch (casetype)
    {
        case casetype_INFORMAL:
            FormSwitcher._redirectToForm("Informal Case", true);
            break;
        case casetype_FORMAL:
            FormSwitcher._redirectToForm("Formal Case", true);
            break;
        default:
            FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);
            break;
    }
}

Any help is appreciated--thanks! (I am using Microsoft Dynamics 365 Server Version 9.2)