indexOf( ) not working

Sandeep Raj1
Tera Contributor

Hi All,

 

I am trying indexOf () function in inbound action to check the subject and assign to particular group. But indexOf( ) is assigning tickets to only one group. I have 2 almost similar subjects to validate like below, but it is not working. Emails with both subjects are routing to first assignment group only.

 

if(current.subject.toLowerCase().indexOf('This is string one') != -1)

current.assignment_group = "sys_id of group 1";

else if(current.subject.toLowerCase().indexOf('This is string two') != -1)

current.assignment_group = "sys_id of group 2";

 

Please share suggestions on how to handle this.

 

Regards,

Sandeep

 

 

4 REPLIES 4

Sebastian L
Mega Sage

Hey, 

Then your first condition must be met, since it is using that. If you are in a test environment, please try to log out your results and reprocess the same email, so you can track your progress. Log out the result of what the subject looks like and then compare to your condition. 

And remember, if you are using .toLowerCase(), that you of course need to look for a lowercase string. Anywho, I don't see anything wrong with your script and just tried it in a background script to confirm: 

 

var test = 'This is string two'; //email.subject
gs.info('test');

if(test.toLowerCase().indexOf('this is string one') != -1) 
gs.info("sys_id of group 1");

else if(test.toLowerCase().indexOf('this is string two') != -1) 
gs.info("sys_id of group 2");

//Result
//*** Script: test
//*** Script: sys_id of group 2

 

 


Best regards,
Sebastian Laursen

Saurabh Gupta
Kilo Patron
Kilo Patron

Please share the original code that you have written.

 

 


Thanks and Regards,

Saurabh Gupta

jaheerhattiwale
Mega Sage
Mega Sage

@Sandeep Raj1 It should be email.subject not current.subject. So try below updated code

 

if(email.subject.toLowerCase().indexOf('This is string one') != -1)

current.assignment_group = "sys_id of group 1";

else if(email.subject.toLowerCase().indexOf('This is string two') != -1)

current.assignment_group = "sys_id of group 2";

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

courtenay1
Tera Expert

Apart from the subject condition should be using the email object not current i.e. email.subject , the indexOf matching may depend on the varying subject texts. Can you provide examples?

If the match is a "starts with" I often will use trim() and search() instead of indexOf() to test for the starting position of "0" not "-1" . Example test background code ...

 

var current_assignment_group = "";
var email_subject = "  this is string two and this is string one";
if (email_subject.trim().toLowerCase().search('this is string one') == 0) { // 0 if starts with
   current_assignment_group = "sys_id of group 1"; 
} else if (email_subject.trim().toLowerCase().search('this is string two') == 0) {
   current_assignment_group = "sys_id of group 2";
}

gs.info("current_assignment_group: [" + current_assignment_group + "]");
// *** Script: current_assignment_group: [sys_id of group 2]

 

 ... search() also allows regex pattern matching which sometimes helps with non-trivial matching at the expense of obfuscation.