multiple indexof doesnt work

tsoct
Tera Guru

Hello all,

While caller id is included in the allMembers array, I expected to see 'Matches found'. However, when I execute this as a background script, I get 'No matches found'. What might be the reason?

 

var rarray = [];
var ids = [];

var test = new GlideRecord('sys_email');
test.addEncodedQuery('sys_id=840ea1a493b206109a5432847aba1016');
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'));
    }
}
var allMembers = arrayUtil.unique(ids);

//ids return 366ab25b1b50fb00cda74339cd4bcb58
//allMembers return 366ab25b1b50fb00cda74339cd4bcb58

var inc = new GlideRecord("incident");
inc.addQuery('sys_id', '5aed29ac833a06108168dc226daad3a5');
inc.query();
if (inc.next())
    if ((inc.assignment_group == '12344597a63910f991f441f053af1e') && ((allMembers.indexOf(inc.opened_by) > -1) || (allMembers.indexOf(inc.caller_id) > -1) || (allMembers.indexOf(inc.watch_list.nil()) > -1))) {
        gs.info('Matches found.');
    } else {
        gs.info('NO matches found.');
    }

//debug
//inc.opened_by = 3235c15b1b28b9105e9542ede54bcb7e
//inc.caller_id = 366ab25b1b50fb00cda74339cd4bcb58
//inc.watch_list =

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@tsoct 

 

Here is the fixed script.

 

var rarray = [];
var ids = [];

var test = new GlideRecord('sys_email');
test.addEncodedQuery('sys_id=840ea1a493b206109a5432847aba1016');
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'));
    }
}
var allMembers = arrayUtil.unique(ids);

//ids return 366ab25b1b50fb00cda74339cd4bcb58
//allMembers return 366ab25b1b50fb00cda74339cd4bcb58

var inc = new GlideRecord("incident");
inc.addQuery('sys_id', '5aed29ac833a06108168dc226daad3a5');
inc.query();
if (inc.next())
    if ((inc.assignment_group == '12344597a63910f991f441f053af1e') && ((allMembers.indexOf(inc.opened_by.toString()) > -1) || (allMembers.indexOf(inc.caller_id.toString()) > -1) || (allMembers.indexOf(inc.watch_list.nil()) > -1))) {
        gs.info('Matches found.');
    } else {
        gs.info('NO matches found.');
    }

//debug
//inc.opened_by = 3235c15b1b28b9105e9542ede54bcb7e
//inc.caller_id = 366ab25b1b50fb00cda74339cd4bcb58
//inc.watch_list =

You forgot to add toString() in inc.opened_by and inc.caller_id in the if statement due to which match is failing.

 

Also, I have tested this via a background script on my PDI, hence no further validation from someone else is needed.

 

Hope this helps.

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@tsoct 

 

Here is the fixed script.

 

var rarray = [];
var ids = [];

var test = new GlideRecord('sys_email');
test.addEncodedQuery('sys_id=840ea1a493b206109a5432847aba1016');
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'));
    }
}
var allMembers = arrayUtil.unique(ids);

//ids return 366ab25b1b50fb00cda74339cd4bcb58
//allMembers return 366ab25b1b50fb00cda74339cd4bcb58

var inc = new GlideRecord("incident");
inc.addQuery('sys_id', '5aed29ac833a06108168dc226daad3a5');
inc.query();
if (inc.next())
    if ((inc.assignment_group == '12344597a63910f991f441f053af1e') && ((allMembers.indexOf(inc.opened_by.toString()) > -1) || (allMembers.indexOf(inc.caller_id.toString()) > -1) || (allMembers.indexOf(inc.watch_list.nil()) > -1))) {
        gs.info('Matches found.');
    } else {
        gs.info('NO matches found.');
    }

//debug
//inc.opened_by = 3235c15b1b28b9105e9542ede54bcb7e
//inc.caller_id = 366ab25b1b50fb00cda74339cd4bcb58
//inc.watch_list =

You forgot to add toString() in inc.opened_by and inc.caller_id in the if statement due to which match is failing.

 

Also, I have tested this via a background script on my PDI, hence no further validation from someone else is needed.

 

Hope this helps.

Million thanks! @Sandeep Rajput 

@tsoct You forgot to mark the answer helpful. Also, in the previous thread my answer was partially correct as despite fixing the allMembers variable the script still would have returned the incorrect result if 

ids.push(getuserID.getValue('sys_id'));

Wasn't used.

Thanks @Sandeep Rajput 😊