Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

OnChange client script not working

MonicaW
Tera Guru

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.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.setValue('record_needs_updating', 'false');
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');

// else g_form.setValue('record_needs_updating', 'false');
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@MonicaW 

try this

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

    var needsUpdate =
        !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', needsUpdate);
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

pavani_paluri
Tera Guru

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 it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

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?  

Tanushree Maiti
Kilo Patron

Hi @MonicaW ,

 

Try this

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.setValue('record_needs_updating'false);
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);

 

// else g_form.setValue('record_needs_updating', 'false');
}
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Thank you for your help.  I tried it and it still fails. I'm going to go back to trying UI policies for this as I know they execute before client scripts.  I had tried them earlier, but ran into an issue - but I think I've figured out what to do.  I believe the reason the onchange script isn't working is because when I select the configuration item, I have it auto-populating the fields and I believe, this change is conflicting with the onchange script.  Thanks again.