- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 08:07 AM
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!');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 08:36 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 08:34 AM
@tsoct Please update the script as follows and see if it works.
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.getValue('sys_id')); //Changed this line
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!');
}
}
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 08:52 AM
Hi @tsoct
are you sure, that above script works, as it is definitely wrong and should not be posted here as correct solution! For the reason see my initial response!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 08:57 AM
I marked the incorrect one. In reality, I was able to resolve my issue with your response.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:03 PM - edited 06-11-2024 12:04 PM
My bad I didn't see
var allMembers = arrayUtil.unique(ids);
declared inside the while loop
However the following code
while (getuserID.next()) {
ids.push(getuserID.sys_id);
}
is still incorrect as it would replicate the sys_id of the last element through out the array due to call by reference issue.
The correct code would be
while (getuserID.next()) {
ids.push(getuserID.getValue('sys_id')); //returns a unique sys_id string each time.
}
@tsoct I hope I am correct this time.