We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to check string contains certain word in client script

Renu9
Tera Contributor

Hi All,

I am having location and location type variables in 1 catalog item. Based on location variable, I need to set location type variable(remote, onsite) choices in select box.

I am writing an on change client script . Requirement is if location variable contains ex: Remote, AZ or Home Office(California), then the location type should appear as Remote

Any other selection, it should be Onsite. 

But as per the below script, it is always going to Onsite . Please help me how it can be achieved

 

 var loc = g_form.getDisplayValue('location_emp');
    alert("loc is " + loc);
    if ((loc.toLowerCase().contains('remote') || loc.toLowerCase().contains('home office')) > -1) {
        alert("into if");
        g_form.setValue('location_type', 'Remote');
    } else {
        alert("into else");
        g_form.setValue('location_type', 'Onsite');
    }
}
5 REPLIES 5

Amitoj Wadhera
Kilo Sage

Hey @Renu9 ,

 

Please find the corrected version of your script:

var loc = g_form.getDisplayValue('location_emp').toLowerCase();
alert("loc is " + loc);
if (loc.includes('remote') || loc.includes('home office')) {
    alert("into if");
    g_form.setValue('location_type', 'Remote');
} else {
    alert("into else");
    g_form.setValue('location_type', 'Onsite');
}

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera