- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:00 AM
Hi,
I have two variables - test and target .both contains a list of sys_Id separated by comma. Now I have to write a business rule which compare the sys_ids in test with that of the target and will throw an error message if any of the sys_id in test is not present in the target. How can I write a script for this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 05:17 PM
Hi Maharshi,
Fields "test" and "target" are both of type "list".
var testArray = test.split(',');
var targetArray = target.split(',');
var arrayUtil = new ArrayUtil();
var intersect = arrayUtil.intersect(targetArray, testArray);
if (intersect.length != testArray.length) {
gs.addErrorMessage('There are some items in test array that is not in target array.');
current.setAbortAction(true);
I've tested the script using the following code to make sure it works as expected.
var target = '4f1764429780111086d3b4b3f153afba,4f1764429780111086d3b4b3f153afbd,4f1764429780111086d3b4b3f153afbc,62826bf03710200044e0bfc8bcbe5df1,a8f98bb0eb32010045e1a5115206fe3a'
var grTable = new GlideRecord('a0_table2');
if (grTable.get('4f1764429780111086d3b4b3f153afbc')) {
var test= grTable.u_list_field; // field of type 'list'
var testArray = test.split(',');
var targetArray = target.split(',');
var arrayUtil = new ArrayUtil();
var intersect = arrayUtil.intersect(targetArray, testArray);
if (intersect.length != testArray.length) {
gs.addErrorMessage('There are some items in test array that is not in target array.');
current.setAbortAction(true);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 07:05 AM
Yes, so your a1 will be test, and a2 will be target.
No matter the size of array, this will work. just make sure your list is an array.
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 12:01 AM
Did you try this, this should work for your req
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2022 03:47 AM
Hey,
I suggested using intersect in the beginning itself, but glad that you found the solution and ignored this one
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:28 AM
Hi Maharshi,
You can use below snippet, it returns true if all values of list one is present in list two, otherwise false.
var target = ['B', 'C', 'A', 'D'];
var test = ['D', 'C'];
var result = test.every(function(val) {
return target.indexOf(val) >= 0;
});
gs.log(result);
Mark my response correct/helpful as applicable,
Thanks and Regards,
Rahul
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:58 AM
Hi,
var target = [ 'D', 'C'];
var test = ['D', 'C'];
var result = test.every(function(val) {
return target.indexOf(val) >= 0;
});
gs.log(result);
For this input also it is giving result = true