delete members from watchlist on INC/RITM table

DPrasna
Tera Contributor

Hi All,

 

I have 2 business rules that runs on INC/RITM to add watchlist members every time a CI is changed or updated.

The below script queries a rel table and gets the members and adds to the watchlist field.

 

var user_ids = [];
	var grWatchlist = new GlideRecord('u_m2m_from_cmdb_ci_service_watchlist');
	grWatchlist.addQuery('u_cmdb_ci_service', current.getValue('business_service')); 
	grWatchlist.query();
	while(grWatchlist.next()){
		user_ids.push(grWatchlist.getValue('u_user')); 
	}
	current.watch_list = user_ids.join(',');

 

Now the requirement is to also delete the members from watchlist field if any of the users are being deleted from the above custom table. I started with the below Business Rule and stuck with the logic.

 

(function executeRule(current, previous /*null when async*/ ) {
    var table = ["incident", "sc_req_item"];
    for (var i = 0; i < table.length; i++) {
        var gr = new GlideRecord(table[i]);
        gr.addQuery('business_service', current.u_cmdb_ci_service);
        gr.query();
        while (gr.next()) {
            //gr.deleteRecord();
        }
    }
})(current, previous);

Need help with the same.

TIA.

 

@Tai Vu @Runjay Patel @Uncle Rob @Brad Bowman  @Community Alums 

8 REPLIES 8

Uncle Rob
Kilo Patron

watch_list is a glide_list, not a table, so the aim here isn't to "delete records"

You need to build the watch list as you want, and then over-write the previous value.


Hi Rob,

 

Thanks for the reply can I get to see any example that I can follow please.

 

 

Runjay Patel
Giga Sage

Hi @DPrasna ,

 

If you wanna make watch list empty and then add the new members in it then use below script.

var table = ["incident", "sc_req_item"];
    for (var i = 0; i < table.length; i++) {
        var gr = new GlideRecord(table[i]);
        gr.addQuery('business_service', current.u_cmdb_ci_service);
        gr.query();
        while (gr.next()) {
            gr.watch_list = ''; // Set the watch_list field to empty
    gr.update();
        }
    }

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Here in this Video, I have covered the Custom Application Pattern Troubleshooting and configuration Thank you for visiting my channel. Here, I'll share various technical knowledge. Feel free to reach out to me directly for any Service Now-related queries. Your support encourages me to consistently

Hi Runjay, thanks for the reply how do I do it as I have 2 different business rules for adding the members