The CreatorCon Call for Content is officially open! Get started here.

Script error in function using indexOf()

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi guys,

I have a problem with the below function for parse a string and extract a part of it

function grabContent(str, start, end) {   
	var startLen = start.length;   
	var s = str.indexOf(start);   
	var e = str.indexOf(end);   
	var scrape = str.substring(s+startLen, e);   
	return scrape;   
}

I got the following error message and I'm not able to have the expected result, could you please help me?

org.mozilla.javascript.EcmaError: Cannot find function indexOf in object [object EmailWrapper].
Caused by error in sysevent_in_email_action.d63f1540c3123100d5907bfaa2d3aebc.script at line 8

6: function grabContent(str, start, end) { 
7: var startLen = start.length; 
==> 8: var s = str.indexOf(start); 
9: var e = str.indexOf(end); 
10: var scrape = str.substring(s+startLen, e); 
11: return scrape; 

Thanks

Alberto

1 ACCEPTED SOLUTION

Weird one. toString is native javascript, shouldn't be excluded from scope. Try putting it in the background as Harsh suggested and you could try with the String() function as well:

function grabContent(str, start, end) { 
var strString = String(str);
var startString = String(start);
var endString = String(end);  
	var startLen = startString.length;   
	var s = strString .indexOf(startString);   
	var e = strString .indexOf(endString);   
	var scrape = strString .substring(s+startLen,e);   
	return scrape;   
}

View solution in original post

14 REPLIES 14

Harsh Vardhan
Giga Patron

convert start to like 

 

var abc= start.toString(); and then add str.indexOf(abc);

Anurag Tripathi
Mega Patron
Mega Patron

Hi Alberto,

 

This looks like a type case issue.

Add .toString() to str so it becomes

str.toString().indexOf(start);

 

-Anurag

 

-Anurag

I did it, now I got the following error:

java.lang.SecurityException: Method returned an object of type IdFunctionObject which is not allowed in scope sn_customerservice
Caused by error in sysevent_in_email_action.d63f1540c3123100d5907bfaa2d3aebc.script at line 8

6: function grabContent(str, start, end) { 
7: var startLen = start.length; 
==> 8: var s = str.toString().indexOf(start); 
9: var e = str.toString().indexOf(end); 
10: var scrape = str.substring(s+startLen, e); 
11: return scrape; 

Do you know how to fix it?

Thanks again 🙂

Alberto

convert all your function args to string first.

function grabContent(str, start, end) { 
var strString = str.toString();
var startString = start.toString();
var endString = end.toString();  
	var startLen = startString.length;   
	var s = strString .indexOf(startString );   
	var e = strString .indexOf(endString );   
	var scrape = strString .substring(s+startLen, e);   
	return scrape;   
}