I need to use startsWith() function on a client script, but I got an error

SN Emy
Kilo Sage

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?

 

1 ACCEPTED SOLUTION

SN Emy
Kilo Sage

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');


}
}

View solution in original post

13 REPLIES 13

SanjivMeher
Mega Patron

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?

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

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.

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);

}