arrayUtil. unique() function not giving unique results from an array

Rachna S
Tera Guru

Hello All,

I am trying to concatenate two arrays and then eliminating duplicate sys_id's from the result to give me a list of unique sys_id's. However the function 'unique' does not seem to be working for me.

Here is what the script looks like-

(function executeRule(current, previous /*null when async*/) {
var ptask = new GlideRecord('problem_task');
ptask.addQuery('parent',current.sys_id);
ptask.query();
while(ptask.next()){
var prb = new GlideRecord('problem');
prb.addQuery('sys_id',ptask.parent);
prb.query();
if(prb.next()){
var BS = [];
BS.push(prb.business_service.toString());
}
var BS1 = [];

BS1.push(ptask.business_service.toString());

var b_service = [];
var finlist;
var arrayUtil = new ArrayUtil();


b_service = arrayUtil.concat(BS,BS1);
finlist = arrayUtil.unique(b_service);
var finlist1 = finlist.toString();

ptask.business_service = finlist1;
ptask.update();
}
})(current, previous);

Array BS contains - [A, B, C] 

Array BS1 contains - [B,C,D]

Expected result - [A,B,C,D]

Current Result - [A,B,C,B,C,D]

Can you please tell me what could be wrong with the script here ?

 

Thank you

1 ACCEPTED SOLUTION

Hi Ankur,

The following code finally worked for me. Highlighted the changes I had to make. Thanks for all your help though.

(function executeRule(current, previous /*null when async*/) {
var ptask = new GlideRecord('problem_task');
ptask.addQuery('parent',current.sys_id);
ptask.query();
while(ptask.next()){
var prb = new GlideRecord('problem');
prb.addQuery('sys_id',ptask.parent);
prb.query();
if(prb.next()){
var BS = [];

BS.push(prb.business_service.toString());

}

BS.push(ptask.business_service.toString());
var b_service;

var arrayUtil = new ArrayUtil();
var list = BS.toString();
var array = list.split(",");

var finlist = arrayUtil.unique(array);

ptask.business_service = finlist.toString();
ptask.update();
}
})(current, previous);

View solution in original post

11 REPLIES 11

Steven Parker
Giga Sage

Possibly because you have finlist just declared as a var.  Try doing this first:

var finlist = [];

 

I believe you could've just done this too:

b_service = arrayUtil.concat(BS,BS1);
b_service = arrayUtil.unique(b_service);


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

Thank you for your response. I tried that already. It still gives duplicates.

Allen Andreas
Administrator
Administrator

Hello,

Please check your formatting as appropriate and refer to documentation such as: https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_ArrayUtilAPI#r_AU-un...

Using your array examples with script:

var array1 = ['A','B','C'];
var array2 = ['B','C','D'];
var arrayUtil = new ArrayUtil();
var arrayConcat = arrayUtil.concat(array1, array2);
var arrayUnique = arrayUtil.unique(arrayConcat);
gs.info(arrayUnique);

Which you can run in background script just to see it working, nets you:

find_real_file.png

Please verify your values, use log entries if needed for troubleshooting and reference above script for example.

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


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

Thanks for your response. The format is right. However, the result returned still contains duplicates.