How to check string contains certain word in client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 07:55 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 08:10 AM
the way how display value works is different in classic and portal
update script as this; I hope your variable names are correct and you are setting correct choice values
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
if (window == null) {
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');
}
}
} catch (ex) {
var loc = g_form.getDisplayBox('location_emp').value;
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');
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2024 04:06 AM
I have kept the below script and it is throwing java script browser error.
The below info message is appearing as below
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2024 04:22 AM
are you trying to use Now Assist VA Conversational catalog?
If yes then there are some limitations in it
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 08:11 AM
Hi @Renu9 ,
Use the string indexOf for checking specifix keyword within the string variable.
var loc = g_form.getDisplayValue('location_emp');
alert("loc is " + loc);
//if ((loc.toLowerCase().contains('remote') || loc.toLowerCase().contains('home office')) > -1) {
if(loc.indexOf("remote") != -1 || loc.indexOf("home office") != -1 ) {
alert("into if");
g_form.setValue('location_type', 'Remote');
} else {
alert("into else");
g_form.setValue('location_type', 'Onsite');
}
}
Note: add the toLower() method with loc.
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution