Client Script Switch statement not working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 12:14 PM
I have a business requirement to set a custom field value on the Cost Plan form based on the entered Cost type on the form and the cost type's expense type that is found on the Cost Type Definition table. To do that, I've written an onChange client script for the field Cost type for the Cost Plan form.
This is what I thought would be the correct approach:
1. Create an onChange client script on the Cost type field on the Cost Plan form.
2. In the Client Script, use GlideAjax to look up the expense type value in the Cost Type Definition table based on the Cost type field value (which is a reference field).
3. Use a switch statement using the combination of Cost type and the expense type to determine what value to set in the custom field on the Cost Plan form.
What's happening is it appears the switch statement is running before the GlideAjax code, even though it is not written that way in the client script. Using alerts, the alert for the switch statement displays first with a null value, then the GlideAjax alert displays with the correct value. Shouldn't the GlideAjax run first?
1st alert that appears:
2nd alert that appears:
Here is my client script:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 12:22 PM
Once you switch focus into the callback function, it doesn't come back out. You'll need to put your switch statement inside your function if you want it to work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:53 PM
Jennifer is right
That would make your script as below
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Category is required if the cost type = replacement fund
var costType = newValue;
var expenseType;
// Get the expense type of the cost type definition
var ctExpenseType = new GlideAjax('Script_Utils');
ctExpenseType.addParam('sysparm_name', 'getExpenseType');
ctExpenseType.addParam('sysparm_resourcetype', costType);
ctExpenseType.getXML(callback);
function callback(response) {
expenseType = response.responseXML.documentElement.getAttribute('answer');
alert('Ajax expense type ' + newValue + ' / ' + expenseType);
switch (newValue) {
case 'b2bed4acdb8357c0cfc53220ad961998': // Special Fund
g_form.setValue('u_reason_category', 'Special Fund');
break;
case '38d32ebedbefa300197c38ff9d9619bd': // Replacement Fund
g_form.setValue('u_reason_category', '');
g_form.setMandatory('u_reason_category', true);
break;
default:
alert('expense type: ' + expenseType);
if(expenseType == 'opex'){
g_form.setValue('u_reason_category', '');
} else {
g_form.setValue('u_reason_category', 'General Fund');
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 04:28 PM
Looking at the code, it seems ajax call is only necessary when costType is not Special Fund or Replacement Fund.
I've made callback into an anonymous function to make it easier to see.
Also, it's not good practice to hardcode sys_id in a script because it makes code maintenance much harder.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Category is required if the cost type = replacement fund
//var costType = newValue;
switch (newValue) {
case 'b2bed4acdb8357c0cfc53220ad961998': // Special Fund
g_form.setValue('u_reason_category', 'Special Fund');
break;
case '38d32ebedbefa300197c38ff9d9619bd': // Replacement Fund
g_form.setValue('u_reason_category', '');
g_form.setMandatory('u_reason_category', true);
break;
default:
// Get the expense type of the cost type definition
var ctExpenseType = new GlideAjax('Script_Utils');
ctExpenseType.addParam('sysparm_name', 'getExpenseType');
ctExpenseType.addParam('sysparm_resourcetype', newValue);
ctExpenseType.getXML(function(response) {
var expenseType = response.responseXML.documentElement.getAttribute('answer');
alert('Ajax expense type ' + newValue + ' / ' + expenseType);
if (expenseType == 'opex') {
g_form.setValue('u_reason_category', '');
} else {
g_form.setValue('u_reason_category', 'General Fund');
}
});
break;
}
}