After insert business rule not working

DPrasna
Tera Contributor

Hi All,

 

The requirement is to update watchlist members from a custom table to INC and RITM table so I created a Business  Rule after insert that updates the INC/RITM record whenever a new member is added to the custom table.

While the BR works and adds new member to the list only if one member is available on the custom table as a watchlist.

Ex - If Bob is a watchlist mem and if I try to add Adam then it works/updates the INC/RITM

The below scenario does not work:

1. I add a CI to INC/RITM which has no WL member and save the record

2. I add a member to the CI which had no WL member but it does not update the mem to the INC/RITM

 

BR as follows:

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

 

Need help. TIA.

 

@Ankur Bawiskar @Dr Atul G- LNG @Ravi Gaurav @Sandeep Rajput 

7 REPLIES 7

Swapna Abburi
Mega Sage
Mega Sage

Hi @DPrasna 

If you have written the business rule to trigger when a record is inserted in custom table then it won't be triggered when Incident or RITM is updated. Please share details of Business rule; which table it is executing, when and conditions.

sunil maddheshi
Tera Guru

@DPrasna 

Before splitting  please check if watchlist is empty, also try using Using .includes(userId) instead of .indexOf(userId) === -1, Can you try with below code:

 

 

 

(function executeRule(current, previous /*null when async*/) {
    gs.log("Watchlist update Business Rule triggered.");

    var ciService = current.u_cmdb_ci_service;
    var userId = current.u_user.toString();

    if (!userId) {
        gs.log("User ID is empty. Skipping update.");
        return;
    }

    function updateWatchlist(table, ciField) {
        var gr = new GlideRecord(table);
        gr.addQuery(ciField, ciService);
        gr.addQuery('active', true);
        gr.query();

        while (gr.next()) {
            var watchlist = gr.watch_list ? gr.watch_list.split(',') : []; // Handle empty watchlist
            if (!watchlist.includes(userId)) {
                watchlist.push(userId);
                gr.watch_list = watchlist.join(',');
                gr.update();
                gs.log("Updated watchlist for record: " + gr.sys_id);
            }
        }
    }

    // Update watchlist for Incident & RITM
    updateWatchlist('incident', 'business_service');
    updateWatchlist('sc_req_item', 'business_service');

    gs.log("Watchlist update completed.");
})(current, previous);

 

 

 

Please mark correct/helpful if this helps you

Viraj Hudlikar
Giga Sage

Hello @DPrasna 

 

Your code is missing the logic to handle cases where the watchlist is initially empty.

(function executeRule(current, previous /*null when async*/) {
    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.addEncodedQuery('active=true');
    incGr.query();
    while (incGr.next()) {
        var watchlist = incGr.watch_list ? 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.addEncodedQuery('active=true');
    ritmGr.query();
    while (ritmGr.next()) {
        var watchlist1 = ritmGr.watch_list ? ritmGr.watch_list.split(',') : [];
        if (watchlist1.indexOf(userId) === -1) {
            watchlist1.push(userId);
            ritmGr.watch_list = watchlist1.join(',');
            ritmGr.update();
        }
    }
})(current, previous);

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.