- 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 06:09 AM
Hi,
I want to prevent them from submitting the record if there is any value in test variable that do not match with the values in target.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:13 AM
Hi,
convert those 2 strings to array and then use ArrayUtil difference function
Sample Script
var string1 = 'abc,xyz,pqr';
var String2 = 'abc,xyz';
var arr1 = string1.split(',');
var arr2 = String2.split(',');
var arrayUtil = new global.ArrayUtil();
var diff = arrayUtil.diff(arr1,arr2);
gs.info(diff);
Output:
[0:00:00.066] Script completed in scope global: script
Script execution history and recovery available here
*** Script: pqr
Demo
Regards
Vaishnavi Lathkar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:32 AM
Hi,
actually my target variable may contain 100 values but my test variable will contain only 4-5 values. I have to check whether all the 4-5 values in test is there in target or not. Will this script work in that case also?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:16 AM
Hey,
you might wanna try ArrayUtils.intersect function for this.
To see how it works, below code snippet should help:
var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
var d = arrayUtil.intersect(a1, a2)
gs.print();
O/P : c
Now, you can perform check, if a1.length == d.length then OK else pass infoMessage
Let me know, if this makes sense.
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions :)
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:33 AM
Hi,
actually my target variable may contain 100 values but my test variable will contain only 4-5 values. I have to check whether all the 4-5 values in test is there in target or not. Will this script work in that case also?