How to use the onChange() method within the onLoad() method

mkader
Kilo Guru

Hello,

I have a catalog client script that uses the onLoad() method to remove mandatory fields on a form load. There is one field that is a "Yes/No" type. If that field is set to "Yes" another field displays and is mandatory. My onLoad() script isn't making that field NOT mandatory. Any suggestions?

Below is what I am currently doing:

function onLoad() {
    var ga = new GlideAjax('getUserInfo');
    ga.addParam('sysparm_name', "isMemberOfGroup");
    ga.getXMLAnswer(function(answer) {
        if (answer.toString() == 'true') {
            g_form.setMandatory('order_type', false);
            g_form.setMandatory('order_date', false);
        }
    });

    if (g_form.getValue('delivery_preference') == 'Yes') {
        g_form.setMandatory('delivery_preferene_notes', false);
    }
}

The part that is supposed to handle the onChange() are the last 2 lines:

if (g_form.getValue('delivery_preference') == 'Yes') {
    g_form.setMandatory('delivery_preferene_notes', false);
}

Anything helps,

Thanks!

1 ACCEPTED SOLUTION

Alok Das
Tera Guru

Hi,

onLoad client script can not be used as onChange but onChange client script can be used as onLoad, So you just need to change your client script from onLoad to onChange and use the below modified script for you

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading) {
		var ga = new GlideAjax('getUserInfo');
		ga.addParam('sysparm_name', "isMemberOfGroup");
		ga.getXMLAnswer(function(answer) {
			if (answer.toString() == 'true') {
				g_form.setMandatory('order_type', false);
				g_form.setMandatory('order_date', false);
			}
		});
	}
	if(newValue===''){
		return;
	}
	else if (g_form.getValue('delivery_preference') == 'Yes')
		g_form.setMandatory('delivery_preferene_notes', false);
	
	else
		g_form.setMandatory('delivery_preferene_notes', true);
}

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok

View solution in original post

10 REPLIES 10

I figured it out. I should've used the glideajax in the onchange as well :(. this took too long for something simple thanks for your help!