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

I thought so, seemed like previously I did the OR and it wasn't working, but that could be because I didn't have toString() perhaps?

Pranay Tiwari
Kilo Guru

Hi,

 

You need to push those value in array..as below,

Actually i don't know your requirement but i will just give you a simple suggest for a use case of array,because your question is related to array.

 

ar rarray = [];
var nrarray =[];

			if
                                  nrarray=nrarray.push(rarray[i]);
		else
			nrarray = rarray[i];

 

if face any issue then use nrarray.push(rarray[i]).toString();

 

Mark correct or helpful if it helps you.

 

Warm Regards,

Pranay Tiwari

| www.DxSherpa.com | pranay.tiwari@dxsherpa.com |

For the array it might be helpful to have the full context of the script. Basically what this is doing is getting recipients and pushing to an array so that they can be added to the Watch List, since many times we have people on email threads that also go to our instance to be processed by inbound rules.

 

 

//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)
	{
	var email_sub = email.subject.split(': ')[1];
	
	var req = new GlideRecord('u_custom_table');
	req.addEncodedQuery('u_duplicate=false^short_description='+email_sub+'^ORshort_description='+email.subject);
	req.query();
	
	if (req.next())
		{
		//var bodyText = email.body_text;
		//if (!bodyText)
			//bodyText = email.body_html;
		req.comments = "Replied by email:\n\nReceived from: " + email.origemail + "\n\n" + email.subject + "\n\n\n" + email.body_text.replace(/\n+From:+.*/, '');
		if (nrarray!='')
			{
			if (req.watch_list!='')
				req.watch_list = req.watch_list+','+nrarray;
			else
				req.watch_list = nrarray;
		}
		req.update();

Jim Coyne
Kilo Patron

I think what you are trying to do is avoid the three addresses from being added to the watchlist, correct?

If so, you could use the "diff" function in the ArrayUtil class for that:

var recipients = email.recipients.toLowerCase().split(",");
var skipAddresses = ["info@companyname.com", "infocompanyname@companyname.com", "instancename+ga@service-now.com"]
var watchList = arrayUtil.diff(recipients, skipAddresses).join(",");

 It will return a new array containing values from the first array passed in, with the items in the second array stripped out.

Thanks Jim, so in this case, where I want to update the Watch List, I would just use the variable watchList?

If my thinking is correct, the code below could then be removed since the var watchList is already defining the array without the specified addresses?

for (var i=0; i<rarray.length; i++) {
	if (rarray[i] != info1 || info2 || info3) {
		if (nrarray)
			nrarray=nrarray+','+rarray[i];
		else
			nrarray = rarray[i];
	}

And then later on in the script, when setting the Watch List it would be as simple as setting the glide record's Watch List to the watchList variable?

			if (req.watch_list!='')
				req.watch_list = watchList
		}
		req.update();