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

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Thanks David, just tried it...same issue:

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: 
7: function grabContent(str, start, end) { 
==> 8: var strString = str.toString(); 
9: var startString = start.toString(); 
10: var endString = end.toString(); 
11: var startLen = startString.length; 

There must be something else but I'm lost at the moment 😞

im beginning to suspect if it is something to do with the scoped app.

-Anurag

can you run the same script in background script and select your application scope

and let us know what exactly its returning here.  

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

gs.info(grabContent('hey','hello','hi')); // add the parameter value here

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

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

In backgroun script, it works fine...

find_real_file.png