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.

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

@Rachna S 

Any update on this?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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);