Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Return all users whose emails are in the array

tsoct
Tera Guru

The below is part of the script include that failed to return the list of users. 

How can I return a list of users based on an array of email addresses?

 

                var email = []; //this contains (abc@org.com,efg@org.com,hij@org.com)
                var emailArr = email.split(',');
                var userArr = [];

                for (var i = 0; i < emailArr.length; i++) {
                    var grUser = new GlideRecord('sys_user');
                    grUser.addQuery('email', emailArr[i]);
                    grUser.query();

                    if (grUser.next()) {
                        userArr.push(grUser.getUniqueValue());
                    }
                }

                var requester = userArr.join(',');
                return (requester.toString());

 

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi, in your script you are splitting the array email', and then trying to loop over the result,
but split() is a string method and so the result of the split is undefined.
As you already have a defined array, you can loop through its elements without having to make any changes.

Testing in PDI background script

var email = ['admin@example.com','abel.tuter@example.com', 'lucy.barnes@example.com', 'anthony.roy@servicneow.com']; //this contains (abc@org.com,efg@org.com,hij@org.com)
var emailArr = email.split(',');
gs.info('emailArr ' + typeof emailArr);
var userArr = [];

for (var i = 0; i < email.length; i++) {
    var grUser = new GlideRecord('sys_user');
    grUser.addQuery('email', email[i]);
    grUser.query();

    if (grUser.next()) {
        userArr.push(grUser.getUniqueValue());
    }
}

gs.info(userArr.toString());

//var requester = userArr.join(',');
//return (requester.toString());

View solution in original post

1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi, in your script you are splitting the array email', and then trying to loop over the result,
but split() is a string method and so the result of the split is undefined.
As you already have a defined array, you can loop through its elements without having to make any changes.

Testing in PDI background script

var email = ['admin@example.com','abel.tuter@example.com', 'lucy.barnes@example.com', 'anthony.roy@servicneow.com']; //this contains (abc@org.com,efg@org.com,hij@org.com)
var emailArr = email.split(',');
gs.info('emailArr ' + typeof emailArr);
var userArr = [];

for (var i = 0; i < email.length; i++) {
    var grUser = new GlideRecord('sys_user');
    grUser.addQuery('email', email[i]);
    grUser.query();

    if (grUser.next()) {
        userArr.push(grUser.getUniqueValue());
    }
}

gs.info(userArr.toString());

//var requester = userArr.join(',');
//return (requester.toString());