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

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

MonicaW
Tera Guru

Hi Ankur, I went with UI policies instead but I believe your solution would have worked.  I found a mis-matched variable type that was causing the issues.  I didn't test your solution, but I will mark it as accepted.