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

@Alok Das - Thanks for clarifying that for me. I did not know onChange cannot be used within onLoad. However, this is still not working. I added an alert to ensure I was getting in else if condition, and I was, but the field is not being set to false for mandatory.

There is already a UI Policy that sets all of our fields to mandatory for this particular catalog item. Will I have to remove the UI Policy in order for this to work? The other mandatory fields that are getting marked as false onLoad also have a UI policy that sets them to mandatory, but the catalog client script works for those fields

Just remove the UI POlicy action for the same field which you want to handle in client script from the ui policy. Also, make sure that the mandatory checkbox is unchecked in the variable.

So I tried that, and it still did not work. The field that we are setting as Mandatory is supposed to be a hidden field that only displays if the answer to a Previous question is Yes. This makes the field display after a response is selected and regardless of the answer to the question. I believe it is because a mandatory field cannot be hidden outside of a UI Policy Action as it is requierd. That's just my guess

You need to set the field as non mandatory before hiding it otherwise it will not allow the user to save/submit /update the record. Use function setMandatory() to handle the mandatory of field and use setVisible() to handle the visibility of the field.

Okay so now I finally got it to set mandatory from the script, but it is setting the fields to mandatory for all users. I need it to only be mandatory for certain users when the field changes. Also, how do you reverse if false?