Help on array / indexOf in script

Marcel H_
Tera Guru

Hoping someone can provide some guidance on an issue I'm trying to solve. I have a script in an inbound action that looks at recipients, puts them in an array and then looks for specific values. I thought I had found a way to look for multiple values in the array using indexOf, but couldn't seem to get it to work the way I thought it should. Is there a way in an indexOf check to check for value1 OR value2 OR value 3?

Below is my code. I ended up just looking for one of the values I needed (the most common) but there are times when people mail to one of the other addresses too. Any help is greatly appreciated!

//Note: current.opened_by is already set to the first UserID that matches the From: email address

var rarray = email.recipients.toLowerCase().split(",");
var nrarray = '';
var info1 = 'info@companyname.com';
var info2 = 'infocompanyname@companyname.com';
var info3 = 'instancename+ga@service-now.com';

for (var i=0; i<rarray.length; i++) {
	if (rarray[i] != info2) {
		if (nrarray)
			nrarray=nrarray+','+rarray[i];
		else
			nrarray = rarray[i];
	}
}
if (email.recipients.toLowerCase().indexOf('infocompanyname@companyname.com')>-1)
1 ACCEPTED SOLUTION

You can just add an OR

if (email.origemail.toString().toLowerCase().indexOf("@example.com") > -1 || email.origemail.toString().toLowerCase().indexOf("@company.com") > -1 )

View solution in original post

12 REPLIES 12

Brian Lancaster
Tera Sage

Index of should work.  You may need to add to string to it.  Try doing 

if (email.recipients.toString().toLowerCase().indexOf('infocompanyname@companyname.com')>-1)

Brian Lancaster
Tera Sage

Also since this is an inbound action you should not need to pull the array of recipients into another array and just do the index of in email.recipients make sure you have to string in there.  I have one with the following code to check who it was sent from.

if (email.origemail.toString().toLowerCase().indexOf("@example.com"))

In the case of using indexOf and looking for "@example.com", is there a good way to also look for a second string? We have two addresses that would use the @companyname.com and a third that uses a Plus Address from our instance, so would have @service-now.com.

You can just add an OR

if (email.origemail.toString().toLowerCase().indexOf("@example.com") > -1 || email.origemail.toString().toLowerCase().indexOf("@company.com") > -1 )