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

Brad Bowman
Kilo Patron
Kilo Patron

How did you make comments mandatory - dictionary entry, dictionary override, client script, UI Policy...?  Is this on an table extended from task or a custom table?

This is on the task_time_worked table and comments is set to mandatory via dictionary entry. 

JaredWason_0-1731446389525.png

 

Just to clarify, in a test case are you seeing the 3 choices removed, comments populated with 'TEST', but it's still mandatory?  Do you happen to have any other client scripts or UI Policies affecting this field?

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