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 - LNG @Ravi Gaurav @Sandeep Rajput 

7 REPLIES 7

Ankur Bawiskar
Tera Patron

@DPrasna 

try this

(function executeRule(current, previous /*null when async*/) {
    var ciService = current.u_cmdb_ci_service;
    var userId = current.u_user.toString();
    
    // Create an array to hold incidents and RITMs to update
    var recordsToUpdate = [];

    // Query for both incidents and RITMs in one go
    var gr = new GlideRecord('incident');
    gr.addQuery('business_service', ciService);
    gr.addEncodedQuery('active=true');
    gr.query();

    while (gr.next()) {
        var watchlist = gr.watch_list ? gr.watch_list.split(',') : [];
        if (watchlist.indexOf(userId) == -1) {
            watchlist.push(userId);
            recordsToUpdate.push({ table: 'incident', sys_id: gr.sys_id, watch_list: watchlist.join(',') });
        }
    }

    // Query for RITMs
    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);
            recordsToUpdate.push({ table: 'sc_req_item', sys_id: ritmGr.sys_id, watch_list: watchlist1.join(',') });
        }
    }

    // Batch update all records
    for (var i = 0; i < recordsToUpdate.length; i++) {
        var record = new GlideRecord(recordsToUpdate[i].table);
        if (record.get(recordsToUpdate[i].sys_id)) {
            record.watch_list = recordsToUpdate[i].watch_list;
            record.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.