Script to check email recipient

tsoct
Tera Guru

Hello all,

 

I'm trying to determine whether the email recipient or the requestor is reciepient of email. In the script below, the rarray returns a list of emails, but ids only returns one sys_id. What might have gone wrong?

var rarray = [];
var ids = [];
var opened by ='08ace6414758f550ae23aa38036d430f';
var requestor = '66910a89c352865060f6de070501312b'

var test = new GlideRecord('sys_email');
test.addEncodedQuery('sys_id=033e3350837206108168dc226daad3c8');
test.query();

if (test.next()) {
    rarray.push(test.recipients.split(','));

    if (test.copied) {
        rarray.push(test.copied.split(','));
    }
    gs.info('The rarray are: ' + rarray);
}

for (var i = 0; i < rarray.length; i++) {
    var recipient = rarray[i];

    var arrayUtil = new ArrayUtil();
    var getuserID = new GlideRecord('sys_user');
    getuserID.addQuery('email', recipient);
    getuserID.query();

    while (getuserID.next()) {

        ids.push(getuserID.sys_id);
        var allMembers = arrayUtil.unique(ids);
    }
    gs.info('The ids: ' + ids);
    gs.info('The allMembers: ' + allMembers);

    if((allMembers.indexOf(opened by) != -1) || (allMembers.indexOf(requestor) != -1))  {
    gs.info ('Reciepient is openedby/requestor!');
}

}

 

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Your block 

while (getuserID.next()) {
        ids.push(getuserID.sys_id);

        var allMembers = arrayUtil.unique(ids);
}

is wrong, as the variable allMembers is defined inside it. However is has to be defined outside, like

while (getuserID.next()) {
        ids.push(getuserID.sys_id);
}

var allMembers = arrayUtil.unique(ids);

 

View solution in original post

7 REPLIES 7

Thanks! @Sandeep Rajput , I have another issue with this script, by the way. Maybe you may check if you are available. Many thanks!

 

https://www.servicenow.com/community/developer-forum/multiple-indexof-doesnt-work/td-p/2960065

@tsoct Just replied with updated script on your thread. Please check

Maik Skoddow
Tera Patron
Tera Patron

Your block 

while (getuserID.next()) {
        ids.push(getuserID.sys_id);

        var allMembers = arrayUtil.unique(ids);
}

is wrong, as the variable allMembers is defined inside it. However is has to be defined outside, like

while (getuserID.next()) {
        ids.push(getuserID.sys_id);
}

var allMembers = arrayUtil.unique(ids);