- 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-14-2022 02:07 AM
Yes , If I am not wrong to interpret,
Each of your test element is present in target element,
It will give true.
Can you please explain the test case? and your expected output.
Regards,
Rahul
Thanks and Regards,
Rahul

- 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 11:00 PM
Hi,
Thanks for your answer. Actually the length of the test and target is not the determining factor here in my requirement. for eg - test = a,b,c target= a,b,g . In this case the length of test and target is same but the "c" in test is not present in target. In that case also there should be an error message. Will the above code work in this condition also?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2022 12:04 AM
Yes, it would.
Note that I'm not comparing length of "test" with "target" but "test" with "intersect".
if (intersect.length != testArray.length) {
Variable "intersect" hold elements that are common to both arrays. In case of test = a,b,c target= a,b,g, variable "intersect" will be "a,b". I'm comparing the number of elements in this variable "intersect" with number of elements in variable "test". Since there are 3 elements is variable "test" and only 2 elements in variable "intersect", the script would abort.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2022 12:09 AM
Hi,
Sample execution result using sample data in the question.
var target = 'a,b,g';
var test= 'a,b,c';
var testArray = test.split(',');
var targetArray = target.split(',');
var arrayUtil = new ArrayUtil();
var intersect = arrayUtil.intersect(targetArray, testArray);
gs.info('intersect:' + intersect);
if (intersect.length != testArray.length) {
gs.info('There are some items in test array that is not in target array.');
} else {
gs.info('All elements in test are in target.');
}
Execution result. Note that variable "intersect" contains only "a,b" so the length would be 2 and would be different from length of variable "test" which is 3.
*** Script: intersect:a,b
*** Script: There are some items in test array that is not in target array.