- 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:03 AM
Since, these are variables you need a check before submission or after submission?
- 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:11 AM
What is the variable type of variables. Text, Reference, List collector, etc.?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 06:18 AM
The variables are list type