Skip to main content

Notifications

Microsoft Dynamics CRM forum
Unanswered

Automatically save and reload form based on field value

Posted on by 10

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)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,469 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans