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

Hi @DB1 

 

You can use the following logic for Insert in After - Insert BR.

 

(function executeRule(current, previous /*null when async*/) {
	var cuser = current.u_user.sys_id + '';
	var ci = current.u_business_service.sys_id + ''; // Please change u_business_service field name as per your configuration in u_watch_list table
	var incGr = new GlideRecord("incident");
	incGr.addEncodedQuery("watch_listLIKE" + cuser + '^cmdb_ci=' + ci);
	incGr.query();
	
	while(incGr._next()){
		var wl = incGr.getValue("watch_list") + '';
		if(wl.indexOf(cuser) == -1){
			wl = wl + "," + cuser;
		}
		incGr.setValue("watch_list", wl);
		incGr.update();
	}
})(current, previous);

 

You can use the following logic for Update in After - Update BR.

 

(function executeRule(current, previous /*null when async*/) {
	var cuser = current.u_user.sys_id + '';
	var puser = current.u_user.sys_id + '';
	if(puser == cuser){
		return;
	}
	var ci = current.u_business_service.sys_id + ''; // Please change u_business_service field name as per your configuration in u_watch_list table
	var incGr = new GlideRecord("incident");
	incGr.addEncodedQuery("watch_listLIKE" + cuser + '^cmdb_ci=' + ci);
	incGr.query();
	
	while(incGr._next()){
		var wl = incGr.getValue("watch_list") + '';
		wl = wl.replace(puser, cuser);
		incGr.setValue("watch_list", wl);
		incGr.update();
	}
})(current, previous);

 

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

Thanks,
Anvesh

Thanks again!! I tried the below code

After - Insert

It adds the new User if I try to add/ Insert new record. However it does not bring up or push the existing Users

(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") + '';
		if(wl.indexOf(cuser) == -1){
			wl = wl + "," + cuser;
		}
		incGr.setValue("watch_list", wl);
		incGr.update();
	}

})(current, previous);

@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

Tai Vu
Kilo Patron
Kilo Patron

Hey @DB1 

Let's try the below script for your on delete business rule.

 

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

	var userID = current.getValue('u_user');
    var grIncident = new GlideRecord('incident');
    grIncident.addQuery('watch_list', 'LIKE', userID); //replace your User field name
    grIncident.addQuery('cmdb_ci', current.getValue('u_ci'); //replace your CI field name
    grIncident.addActiveQuery();
    grIncident.query();
    while (grIncident.next()) {
        var arrUsers = grIncident.watch_list.split(',');
		arrUsers.splice(arrUsers.indexOf(userID));
		grIncident.setValue('watch_list', arrUsers.join(','));
		grIncident.update();
    }

})(current, previous);

 

 

Seems this is related to an own post.

Need help with Business Rule to insert members to watchlist on Incident

Please consider to mark our comments as solution if it can make your life easier. 😉 

 

Cheers,

Tai Vu