g_form.SetMandatory not working in onChange client script

Jared Wason
Tera Guru

Hi, I have an onChange client script that is adjusting some options and it works great. I am trying to now also make it so that the comments field is no longer required. On line 25 I added 'g_form.setMandatory('comments', false);' like I have in other client scripts, however this is not working. Any reasons why this doesn't work?

 

 

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

	// get user title
	var ga = new GlideAjax('FormCheckUtils');
	ga.addParam('sysparm_name','recordFieldsClientDisplay');
	ga.addParam('sysparm_tablename','u_itd_employee_cost_centers');
	ga.addParam('sysparm_fields', 'u_employee_cost_center');
	ga.addParam('sysparm_query', 'sys_id='+newValue);
	// 	specify callback Function
	ga.getXML((process));
	
	function process(response) {
		var value = response.responseXML.documentElement.getAttribute("answer");
		value = value.split(',');
		var cost_center = value[0];
		if (cost_center.toUpperCase().indexOf("PTOT") != -1) {
			g_form.showFieldMsg('u_event_code','This field is not mandatory but can make ESS reporting easier - TEST');
			g_form.removeOption('u_event_code', 'HRPAY');
			g_form.removeOption('u_event_code', 'HREXP');
			g_form.removeOption('u_event_code', 'NTPAY');	
			
			g_form.setMandatory('comments', false);
			g_form.setValue('comments', 'TEST');
		}
		else {
			g_form.setVisible('u_event_code', false);
		}
	}
}

 

1 ACCEPTED SOLUTION

Just figured it out, cleared my browser cache and it started working properly. Thanks for your assistance!

View solution in original post

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi @Jared Wason Try inserting try-catch in the process function to make sure there is no error before the setMandatory. It may be that the Script Include isn't properly returning a csv data list.

    function process(response) {
        try {
            var value = response.responseXML.documentElement.getAttribute("answer");
            value = value.split(',');
            var cost_center = value[0];
            if (cost_center.toUpperCase().indexOf("PTOT") != -1) {
                g_form.showFieldMsg('u_event_code', 'This field is not mandatory but can make ESS reporting easier - TEST');
                g_form.removeOption('u_event_code', 'HRPAY');
                g_form.removeOption('u_event_code', 'HREXP');
                g_form.removeOption('u_event_code', 'NTPAY');

                g_form.setMandatory('comments', false);
                g_form.setValue('comments', 'TEST');
            } else {
                g_form.setVisible('u_event_code', false);
            }
        } catch (err) {
            alert(err.message);
        }