
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 10:58 AM
Hi,
I need to use startsWith() function on a client script, but I got this error.
onChange script error: TypeError: location.startsWith is not a function function () { [native code] }
please help?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2019 09:17 AM
I fixed it, I have to add .name to the location.
Than you all.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var location = g_form.getReference('u_fac_building');
//g_form.addInfoMessage(location.name);
if(location.name.substring(0,4) == "0152"){
g_form.setValue('assigned_to','442d90fa4f4f8b003aeb58211310c784');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 11:07 AM
startsWith doesn't work in client script. Use indexOf instead.
Can you post your script, so that I can understand, what are you trying to do?
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 11:21 AM
Sanjiv,
There are 14 instances of the startsWith() function in a client script in ServiceNow Madrid. I wrote a simple onChange script that works great in the native view but does not appear to be supported in mobile (ServicePortal).
Thanks,
Derrick Johnson

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 11:24 AM
Yes. I prefer using indexOf(), because i know it works in all types of browser.
I know startsWith() is not supported in certain browser for ex ie11 or mobile as you said. So I wouldn't take risk using it.
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 01:09 PM
I changed that to indexOf, but still got the same error.
onChange script error: TypeError: location.indexOf is not a function function () { [native code] }
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var location = g_form.getReference('u_fac_building');
var str=location.indexOf("0152");
g_form.addInfoMessage(str);
}