push unique values in list collector by comparing caller, manger and watchlist

Dinesh
Tera Guru

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.

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Dinesh 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Tushar
Kilo Sage
Kilo Sage

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