OnChange client script not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
I'm trying to get an onChange client script to examine a group of fields and if ANY of them is blank (have them setup to auto-fill from a table), then I want a checkbox to evaluate to true. Here's what I have but it is not working. Any help/pointers are appreciated (I'm not an expert client scripter). Note, using the commented out else doesn't help either.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
Hi @MonicaW ,
The logic is fine, but there are a couple of small things in ServiceNow client scripting that are tripping you up:
1. Checkbox values
In catalog/client scripts, checkboxes are stored as strings: 'true' or 'false'.
So when you call g_form.setValue('record_needs_updating', 'true'), that’s correct. Just make sure the target field really is a checkbox.
2. Script structure
Right now your if statement doesn’t have braces {} around the block, so only the first line after the if executes.
That means your g_form.setValue('record_needs_updating', 'true'); may not be running as expected.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Default to false
g_form.setValue('record_needs_updating', 'false');
// Check if ANY of the fields are blank
if (
g_form.getValue('life_cycle_stage_as') === '' ||
g_form.getValue('life_cycle_stage_status_as') === '' ||
g_form.getValue('business_service_tier_as') === '' ||
g_form.getValue('change_group_as') === '' ||
g_form.getValue('support_group_as') === '' ||
g_form.getValue('business_contact_as') === '' ||
g_form.getValue('managed_by_as') === '' ||
g_form.getValue('description_as') === '' ||
g_form.getValue('data_classification_as') === '' ||
g_form.getValue('regulatory_attribute_as') === '' ||
g_form.getValue('vendor_as') === '' ||
g_form.getValue('installed_as') === ''
) {
g_form.setValue('record_needs_updating', 'true');
}
}
If you want this to run whenever any of those fields changes, you’ll need to attach the same onChange script to each of them.
Alternatively, you can use an onSubmit script to check all fields at once, which is often simpler.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
Hi Pavani, thank you. I replaced my script with what you gave me above in your reply and it's still not working. I'm not seeing the {} around the IF statement that you mentioned in what you provided though. Should it be right before the word IF?
