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.

duplicate getting pushed to array

Rahul33
Kilo Contributor

I have written the below code. But it is storing the same mail id in array but fetching different id in loop

var final_rec=[];
var grp_mail = new GlideRecord('sys_user_group');

var qc=grp_mail.addQuery('sys_id', 'db53580b0a0a0a6501aa37c294a2ba6b');
qc.addOrCondition('sys_id', 'db53a9290a0a0a650091abebccf833c6');
grp_mail.query();
while (grp_mail.next()) {
var mail=grp_mail.email;
gs.info(mail);
final_rec.push(mail);
}
gs.info(final_rec);

Please see the output:

find_real_file.png

1 ACCEPTED SOLUTION

DrewW
Mega Sage

My first thought is you did not account for the fact that grp_mail.email is an object that is being updated every time thru the loop.  So you need to use 

var mail = grp_mail.email.toString();

View solution in original post

2 REPLIES 2

Ziad Qadora
Kilo Sage

Hi,

You do need to check if the entry already exists before pushing into the array, please try the code below.

 

var final_rec = [];
var grp_mail = new GlideRecord('sys_user_group');

var qc = grp_mail.addQuery('sys_id', 'db53580b0a0a0a6501aa37c294a2ba6b');
qc.addOrCondition('sys_id', 'db53a9290a0a0a650091abebccf833c6');
grp_mail.query();
while (grp_mail.next()) {
    var mail = grp_mail.email;
    gs.info(mail);
    if (final_rec.toString().indexOf(mail) == -1) {
        final_rec.push(mail);

    }
}
gs.info(final_rec);

 

Regards,

 

Ziad

Please mark reply as Helpful/Correct, if applicable. Thanks!

DrewW
Mega Sage

My first thought is you did not account for the fact that grp_mail.email is an object that is being updated every time thru the loop.  So you need to use 

var mail = grp_mail.email.toString();