Business logic and solution when deletion of record happens

DB1
Tera Contributor

Hello All,

 

I have the following requirement:

1. There is a custom table (Related List) on Business Service table table name - u_watch_list

DB1_0-1701613136441.png

2. The same user could be part of any Incident

DB1_1-1701613201496.png

3. If that user is removed from the related list or a record is deleted from u_watch_list table then it should check all the Incidents if the user exist as a watchlist member. If yes, them it should remove them from the watchlist.

Need help with achieve the same.

 

TIA

@Timi @Dr Atul G- LNG @Danish Bhairag2 @Ankur Bawiskar 

 

1 ACCEPTED SOLUTION

@DB1 

 

You mean existing watch list users are getting wiped out and only the newly inserted user is only present? If yes, Try the below code.

 

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

	var cuser = current.u_user.sys_id + '';
	var ci = current.u_cmdb_ci_service.sys_id + ''; //
	var incGr = new GlideRecord("incident");
	incGr.addEncodedQuery('business_service=' + ci);
	incGr.query();

	while(incGr._next()){
		var wl = incGr.getValue("watch_list") + '';
		var fwl = wl.split(',');
		var arrayUtil = new ArrayUtil();
		if(arrayUtil.contains(fwl, cuser) == false){
			fwl.push(cuser);
		}
		incGr.setValue("watch_list", fwl.join(','));
		incGr.update();
	}

})(current, previous);

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@DB1 

it should be an easy script. Do you want to search only those Incidents where this CI is present?

BR: Before delete on u_watch_list

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

Hi Ankur, Yes we need to search only the respective CI related Incidents and for the watchlist members present

AnveshKumar M
Tera Sage
Tera Sage

Hi @DB1 

You can use before Delete BR like the one below.

AnveshKumarM_0-1701660843661.png

(function executeRule(current, previous /*null when async*/) {
	var cuser = current.u_user.sys_id + '';
	var incGr = new GlideRecord("incident");
	incGr.addEncodedQuery("watch_listLIKE" + cuser);
	incGr.query();
	
	while(incGr._next()){
		var fwl = [];
		var wl = incGr.getValue("watch_list") + '';
		wl = wl.split(',');
		for(key in wl){
			if(wl[key] != cuser){
				fwl.push(wl[key]);
			}
		}
		incGr.setValue("watch_list", fwl.join(','));
		incGr.update();
	}
})(current, previous);

 

You can also add the CI Condition in the encoded query if you want to remove the user from watch list only for the incidents where the CI is referred. Let me know the field name if you want encoded query for that. 

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

Hi Anvesh, Thanks for the reply I tried and it works as expected for deletion. Can I use the same BR for Update as well meaning when addition happens it has to update the CI because with the current logic works for deletion if watchlist user already present in Incident if not it does not delete and there is no current logic that updates Incident on Insertion. So can I apply befor Insert/ Delete?