Help with making a field mandatory when a certain value has been selected

cstangoe
Mega Guru

Hi All,

 

I need help to make a field mandatory and visible when a certain value has been selected for another field. I know this could be achieved via a UI Policy but I do not wish for that field to be mandatory for existing incidents which already have the value, only when the value has been changed to it.

 

So for example..... The field called 'u_test1' has a choice of 1,2,3,4 when you select choice number 3 the field called 'u_test2' becomes visible and mandatory.

 

Thanks

 

3 REPLIES 3

Kai Tingey
Tera Guru

you can do this with an onChange client script

 

if (newValue == 'value_goes_here'){
g_form.setMandatory('field_name',true); //set specified field to mandatory
}
else
{
g_form.setMandatory('field_name',false); //set it back if value does not match
}

Amit Gujarathi
Giga Sage
Giga Sage

HI @cstangoe ,
I trust you are doing great.
Here's a sample script that you can use:

// Name: Make u_test2 mandatory on u_test1 change
// Table: [Your Table Name] (e.g., incident)
// When to run: onChange
// Field name: u_test1

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

    var test2Field = g_form.getField('u_test2');

    if (newValue == '3') {
        // Make u_test2 visible and mandatory
        g_form.setMandatory('u_test2', true);
        g_form.setDisplay('u_test2', true);
    } else {
        // Make u_test2 not mandatory and hide it
        g_form.setMandatory('u_test2', false);
        g_form.setDisplay('u_test2', false);
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Harish Bainsla
Kilo Patron
Kilo Patron

function onChangeUtest1() {

var uTest1Value = g_form.getValue('u_test1');


if (uTest1Value == 3) {

g_form.setDisplay('u_test2', true);


var uTest2Value = g_form.getValue('u_test2');
if (!uTest2Value) {
g_form.setMandatory('u_test2', true);
}
} else {

g_form.setDisplay('u_test2', false);
g_form.setMandatory('u_test2', false);
}
}