How to update a user related list record when a watch list external user gets added or removed

IB98
Giga Sage

Hello,

is there a way to update a sn_customerservice_related_party related list record when the external users get added/ removed from a sn_customerservice_case watch list ? Thank you!

10 REPLIES 10

phgdet
Mega Sage

@IB98 
1. Create a Related Party Configuration for external users. 

Screenshot 2025-05-28 at 12.00.33.png

2. Create a Business Rule runs on updating sn_customerservice_case record, if the watch_list field changes, create a Related Party Configuration record with the above newly created type.

I HAVE CREATED THE RECORD BUT I MIGHT NEED HELP WITH THE BUSINESS RULE SCRIPT TO UPDATE OR ADD THE USERS. TO THE RELATED LIST

@IB98 , try create an after BR on watch_list changes.

(function executeRule(current, previous /*null when async*/) {

	const externalPartyConfig = "<sys_id external party config>"; 
	var watchListArr = current.watch_list.split(",");	
	var externalPartiesArr = [];
	watchListArr.forEach(function(party){
		var grUserRole = new GlideRecord('sys_user_has_role');
		grUserRole.addQuery("user", party);
		grUserRole.addEncodedQuery("role.nameSTARTSWITHsnc_external");
		grUserRole.query();
		if(grUserRole.next())
		{
			externalPartiesArr.push(party);
		}
	});

	var grRelatedParty = new GlideRecord("sn_customerservice_related_party");
	grRelatedParty.addQuery("case_record", current.sys_id);
	grRelatedParty.addQuery("party_config", externalPartyConfig); 
	grRelatedParty.query();
	while(grRelatedParty.next()){
		var partyId = grRelatedParty.user + "";
		if(partyId && !externalPartiesArr.includes(partyId))
		{
			
			grRelatedParty.deleteRecord();
		} else if (partyId && externalPartiesArr.includes(partyId))
		{
			externalPartiesArr =  externalPartiesArr.filter(function (party) {
				return party !== partyId;
			});
		}
		
	}
	externalPartiesArr.forEach(function(party){
		var grRelatedParty = new GlideRecord("sn_customerservice_related_party");
		grRelatedParty.initialize();
		grRelatedParty.setValue("case_record", current.sys_id);
		grRelatedParty.setValue("party_config", externalPartyConfig);
		grRelatedParty.setValue("user", party);
		grRelatedParty.insert();

	});
	

})(current, previous);

Ankur Bawiskar
Tera Patron
Tera Patron

@IB98 

you can use after update business rule on case table and then modify the watch list

Business Rule: After Update on sn_customerservice_case

Condition: Watch List Changes

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var rec = new GlideRecord('sn_customerservice_related_party');
    rec.addQuery('case_record', current.sys_id);
    rec.query();
    if (rec.next()) {
        var currentUsersArr = rec.watchListField.toString().split(',');
        var watchListFromCaseArr = current.watch_list.toString().split(',');
        var arrayUtil = new global.ArrayUtil();
        var finalArr = arrayUtil.concat(currentUsersArr, watchListFromCaseArr);
        finalArr = arrayUtil.unique(finalArr);
        rec.watchListField = finalArr.toString();
        rec.update();
    }

})(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