Ct111
Giga Sage

Hi All,

Hope you have come across a scenario where you perform certain use cases on ARRAYS  like 

 

1. find element out of the given array 

2. find the difference between two array's and what is the difference value

3. find the index of element if it exists and returns that

4. to merge two arrays without duplicates

5. remove the duplicate item and appear once

 

The good news is we can use the OOB function (ArrayUtil)  of ServiceNow for this and get our results,

you can use this logic in the script include or any other scripting place and accomplish your task. This

array solution is helpful especially in the scenarios where you have fields of a LIST  type.

So, below is the logic for all the above use-cases

 

1. find element out of the given array 

var x = [12,13,56,15,178,23,21];

 var tst = new ArrayUtil().contains(x,15);

 gs.info(tst);

 

2. find difference between two array's and what is the difference value

var a1 = [12,13,56,15,178,23,21];

var a2 = [12,14,56,15,178,24,25];

var tst = new ArrayUtil().diff(a1,a2);

gs.info(tst);

 

3. find if the index of element if it exist and return that

var a1 = [12,13,56,15,178,23,21];

 var tst = new ArrayUtil().indexOf(a1,15);

 gs.info(tst);

 

4. to merge two arrays without duplicates

var a1 = [12,13,56,15,178,23,21];

 var a2 = [14,15,16,17];

 var tst = new ArrayUtil().union(a2,a1);

 gs.info(tst);

 

5. remove duplicate item and appear once

var a1 = [12,13,56,15,178,23,21,13,21,65,56];

 var tst = new ArrayUtil().unique(a1);

 gs.info(tst);

 

I hope these scripts are useful to you all when working with arrays whether it is list or a field in which you

are storing multiple values and out of that you need to check something.

 

Mark my ARTICLE as HELPFUL and BOOKMARK it if you consider it useful.

Comments
Community Alums
Not applicable

Seems like the above usecases can be used in a scenario where we have sys_ids in array.

Ct111
Giga Sage

Yeah it will be valuable in that case.

SR2
Tera Contributor

Hello creativethinker,

Thanks for your help in Arrays,

Can you please help me with the issues I have with arrays, like.

1. How to merge the two arrays and display them in order.


2. I have pushed some "sys_id's " in an array, I need to get all the percentage values related to those sys_id's.

for ex. there are 12 sys_id's and 50 percentages related to them. But, here I'm getting only 5 percentages.

3. And how to find the highest percentage among those 50 percentage's.

Here is the script I have tried to get all the sys_id's

var abc = [];
var gr1 = new GlideRecord("sn_vdr_risk_asmt_assessment");
gr1.addQuery("vendor", "ef8e969d1bc920d0f4f0a6c1b24bcbef");
gr1.query();
while (gr1.next()) {
abc.push(gr1.sys_id.toString());
}

var omg = new GlideRecord("task_sla");
for (var i = 0; i < abc.length; i++) {
omg.addQuery('task', abc[i]);
omg.query();
while(omg.next()) {
gs.info(omg.percentage);
}
}

Can you please help me with those issues.

Thanks in advance.

Sandeep132
Kilo Sage
Hi, Nothing seems to be wrong with your code. Make sure if there are entries for all the 12 sys_id’s you are getting in the 1st while loop in task_sla table. Looks like only 5 vendor risk assessment records have entries in task_sla table. To get highest percentage you can use “omg.orderbyDesc(‘percentage’)” query.
Version history
Last update:
‎03-03-2021 05:25 AM
Updated by: