Add members automatically to watchlist

DPrasna
Tera Contributor

Hi All,

 

The requirement is as follows:

There is a custom table related list on CI - name "u_m2m_from_cmdb_ci_service_watchlist".

So whenever an INC or RITM is updated/ inserted with any of the CI it should pull the watchlist members from the custom table and update the same.

I have below BR written on INC/RITM table so whenever a CI is updated or inserted it adds the watchlist members

(function executeRule(current, previous /*null when async*/ ) {
    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(',');

})(current, previous);

 

However I want the same to happen from the custom table, so whenever a member is added to the custom table "u_m2m_from_cmdb_ci_service_watchlist" it should add the member automatically to the watchlist of INC/RITM how do I do it?

 

@Ankur Bawiskar @Runjay Patel @Ravi Gaurav @Sandeep Rajput @Dr Atul - LNG 

  

4 REPLIES 4

Ankur Bawiskar
Tera Patron

@DPrasna 

use this in after update BR on custom table

Condition: Watch list changes

script:

(function executeRule(current, previous /*null when async*/ ) {
    // Get the CI service from the current record
    var ciService = current.u_cmdb_ci_service;

    // Get the user ID from the current record
    var userId = current.u_user.toString();

    // Update watchlist for INC records
    var incGr = new GlideRecord('incident');
    incGr.addQuery('business_service', ciService);
    incGr.query();
    while (incGr.next()) {
        var watchlist = incGr.watch_list.split(',');
        if (watchlist.indexOf(userId) === -1) {
            watchlist.push(userId);
            incGr.watch_list = watchlist.join(',');
            incGr.update();
        }
    }

    // Update watchlist for RITM records
    var ritmGr = new GlideRecord('sc_req_item');
    ritmGr.addQuery('business_service', ciService);
    ritmGr.query();
    while (ritmGr.next()) {
        var watchlist1 = ritmGr.watch_list.split(',');
        if (watchlist1.indexOf(userId) === -1) {
            watchlist1.push(userId);
            ritmGr.watch_list = watchlist1.join(',');
            ritmGr.update();
        }
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

@DPrasna 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

@DPrasna 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.