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

how are you testing it in your script where this is not working?

 

I'm using it in an Inbound Email Actions script:

find_real_file.png

Here the full script:

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

var case_id = current.sys_id;
gs.info("Case ID: " + case_id);

var old_email = new GlideRecord('sys_email');
old_email.addQuery('type', 'sent');
old_email.addQuery('instance',case_id);
old_email.query();
while (old_email.next()) {
	var full_body = email.body;
	gs.info("HTML FULL BODY OF NEW EMAIL: " + full_body);
	//var html_body = new test();
    //html_body.grabContent(full_body, '</head>', '</html>');
	var html_body = grabContent(full_body, '</head>', '</html>');
	gs.info("HTML BODY OF NEW EMAIL: " + html_body);
}

gs.info("EMAIL SENDER: " + email.origemail);

gs.include('validators');

if (current.getTableName() == "sn_customerservice_case") 
	// truncate the email thread as per 'glide.pop3.reply_separators'
	current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text; 
else
	// insert all the email thread
	current.comments = "reply from: " + email.origemail + "\n\n" + sys_email.body_text; 

//if case.state = resloved (6), then check first word in body for accept or reject
//else if case.state = Awaiting (18), then, move to open state
var case_state = current.state;
if (current.state == 6) {
	var lines = email.body_text.split('\n');
	var firstword = "";
	if (lines.length > 0)
		firstword = lines[0].replace(/^\s+|\s+$/g,'');
	firstline = firstword.toLowerCase();
	if (firstline) {
		if(firstline.indexOf("reject") == 0)
			current.state = 10;
		else if(firstline.indexOf("accept") == 0)
			current.state = 3;
	}
} else if (current.state == 18) {
	current.state = 10;
} else{
	current.sys_updated_on = "";
}

current.update();

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

I tried to use the String() function, now I don't get any error BUT I'm not able to get the grad of the Body content, this is what I got in the logs:

HTML BODY OF NEW EMAIL: [objec

Harsh Vardhan
Giga Patron

can you try with below script and let us know the logs. 

i just changed email.body to email.body_text

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

var case_id = current.sys_id;
gs.info("Case ID: " + case_id);

var old_email = new GlideRecord('sys_email');
old_email.addQuery('type', 'sent');
old_email.addQuery('instance',case_id);
old_email.query();
while (old_email.next()) {
	var full_body = email.body_text;
	gs.info("HTML FULL BODY OF NEW EMAIL: " + full_body);
	//var html_body = new test();
    //html_body.grabContent(full_body, '</head>', '</html>');
	var html_body = grabContent(full_body, '</head>', '</html>');
	gs.info("HTML BODY OF NEW EMAIL: " + html_body);
}

gs.info("EMAIL SENDER: " + email.origemail);

gs.include('validators');

if (current.getTableName() == "sn_customerservice_case") 
	// truncate the email thread as per 'glide.pop3.reply_separators'
	current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text; 
else
	// insert all the email thread
	current.comments = "reply from: " + email.origemail + "\n\n" + sys_email.body_text; 

//if case.state = resloved (6), then check first word in body for accept or reject
//else if case.state = Awaiting (18), then, move to open state
var case_state = current.state;
if (current.state == 6) {
	var lines = email.body_text.split('\n');
	var firstword = "";
	if (lines.length > 0)
		firstword = lines[0].replace(/^\s+|\s+$/g,'');
	firstline = firstword.toLowerCase();
	if (firstline) {
		if(firstline.indexOf("reject") == 0)
			current.state = 10;
		else if(firstline.indexOf("accept") == 0)
			current.state = 3;
	}
} else if (current.state == 18) {
	current.state = 10;
} else{
	current.sys_updated_on = "";
}

current.update();

Sorry my bad, there was a very easy typo in the code, i've put the following and not it works fine!

var full_body = old_email.body;

Anyway the String() function is working fine and that's the correct way to use the grabContent function, so thanks a lot David and all for your support 🙂

Cheers

Alberto