push unique values in list collector by comparing caller, manger and watchlist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 04:53 AM
I have 4 fields referencing to sys_user table on incident form
1.caller(reference)
2.manager(reference)
3.watch_list
4.users(list collector)
I have requirement like all the 1,2,3 field values set in the 4 once record is saved. If1,2 values are available in 3 then only unique values should be set in 4
How can we achieve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 05:20 AM
then do this
1) add all the values from 1,2 and 3 into an array
2) then get unique value from that array and set in field 4
Something like this in before insert/update business rule
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var arr = [];
arr.push(current.getValue('field1'));
arr.push(current.getValue('field2'));
var watchListArray = current.getValue('field3').toString().split(',');
var arrayUtil = global.ArrayUtil();
var finalArr = arrayUtil.concat(arr,watchListArray);
finalArr = arrayUtil.unique(finalArr);
current.setValue('field4', finalArr.toString());
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 05:45 AM
Hi @Dinesh
Something like this should help -
(function executeRule(current, previous /*null when async*/) {
// Get the values from the fields
var caller = current.caller || '';
var manager = current.manager || '';
var watchList = current.watch_list || '';
var users = current.users || '';
// Split the watch_list and users fields into arrays
var watchListArray = watchList.split(',');
var usersArray = users.split(',');
// Add caller and manager values to the usersArray if they are not empty
if (caller) {
usersArray.push(caller);
}
if (manager) {
usersArray.push(manager);
}
// Remove duplicates by converting the array to a Set and back to an array
usersArray = [...new Set(usersArray)];
// Set the updated users field value
current.users = usersArray.join(',');
})(current, previous);
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar