How to keep the old value on change client script

IamSK
Mega Guru

Hello,

I was trying to set back to the old value after alert but some how the alert message won't go after clicking on OK. Need help on my script to get it right.

Requirement was "If the user attempts to select a 'Support Schedule' value other than "24 x 7 x 365", and the Business Significance value is 'Critical' alert them via client script and keep the same value(24 x 7 x 365)"

Below is Onchange client script i tried. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below
if (g_form.getValue('u_business_significance') == 'critical' && g_form.getValue('u_ci_support_schedule') != '38fa64edc0a8016400f4a5724b0434b8') {

alert("Since you have marked this CI as Critical, the Support Schedule must be 24 x 7 x 365. If you want to select a different Support Schedule, then change the Business Significance value to 'Non-critical'.");
g_form.setValue('u_ci_support_schedule',oldValue);
return false;

}
}

Any help is appreciated.

Thank you

 - Sai

4 REPLIES 4

Tony DiRienzo
Giga Guru

You don't need to use g_form.setValue() in this case.  You could just override the newValue like this:

newValue = oldValue;

I believe "return false;" is only effective for cancelling onSubmit() client scripts.

hanaphouse
Giga Guru

Can you take a screenshot of your OnChange client script conditions? You might be missing something - one of the most common mistakes I do is the UI Type (?).

I also made changes to the code to make it cleaner.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var sys_id = '38fa64edc0a8016400f4a5724b0434b8';
var significance = g_form.getValue('u_business_significance');
var support_schedule = g_form.getValue('u_ci_support_schedule');

//Type appropriate comment here, and begin script below
if ( significance == 'critical' && support_schedule != sys_id) {

alert("Since you have marked this CI as Critical, the Support Schedule must be 24 x 7 x 365. If you want to select a different Support Schedule, then change the Business Significance value to 'Non-critical'.");
g_form.setValue('u_ci_support_schedule',oldValue);

}
}

Thanks for suggestion. It help me to modify my code and it worked fine.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}

//Type appropriate comment here, and begin script below
if (g_form.getValue('u_business_significance') == 'critical' && g_form.getValue('u_ci_support_schedule') != '38fa64edc0a8016400f4a5724b0434b8' && newValue != oldValue) {

alert("Since you have marked this CI as Critical, the Support Schedule must be 24 x 7 x 365. If you want to select a different Support Schedule, then change the Business Significance value to 'Non-critical'.");
g_form.setValue('u_ci_support_schedule','38fa64edc0a8016400f4a5724b0434b8');
return false;

}
}

 

 

Jim Coyne
Kilo Patron

I would go in a different direction.  If the CI is marked as Critical, set the Support Schedule automatically to "24 x 7 x 365" and add an info message under the fields (alerts are annoying).

And the reference qualifier on the Support Schedule can be setup to limit the choice to "24 x 7 x 365" when the CI is marked as Critical.

So instead of telling them they did something wrong, guide them in the proper direction.  A much better UX.