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

Hello,

Please review your script for proper formatting and usage. Add log statements as I've mentioned above. You're using toString(), etc. that could be changing results, but again, you would need to add log statements and see what your arrays are reporting as you're going along, etc.

JavaScript isn't picking you out specifically to make this not work, haha...there is an issue here. Please troubleshoot. I've given an example above. Let us know if you need help with log statements.

Thanks!


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

Hi Allen,

 

True that. I did figure out what's missing and have mentioned the same in this thread below. Thanks for all your help.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Optimized it below

1) no need to use 2 arrays as you are anyhow concatenating it afterwards

2) removed unwanted variables to optimize the memory used by variables for holding data

Try this now

(function executeRule(current, previous /*null when async*/) {
	var BS = [];
	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()){
			BS.push(prb.business_service.toString());
		}
		BS.push(ptask.business_service.toString());

		var finlist;
		var arrayUtil = new ArrayUtil();
		finlist = arrayUtil.unique(BS);
		ptask.business_service = finlist.toString();
		ptask.update();
	}

})(current, previous);

Regards
Ankur

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

Hi Ankur,

Thank you for your response. This script also returns the same result.

Hi,

are you in scoped application?

then use this

var arrayUtil = new global.ArrayUtil();

please share logs for your array

Regards
Ankur

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