Assistance Required for Updating Custom Field without refresh in Incident Record via Business Rule.

MihirG
Tera Contributor

Hi Team,

When adding or removing affected CIs in an Incident record, I want a custom field named "List of Associated CIs" to be automatically updated with the list of affected CIs along with their total count. Could you please guide me on how to implement this functionality using a Business Rule in ServiceNow?

 

Note: One part of my implementation is working correctly. When affected CIs are added, the correct counts and CI names are displayed in my custom field. However, during the removal of affected CIs, the custom field does not get updated as expected.

I kindly request you to review my script below and make the necessary modifications. Your assistance would be greatly appreciated!

 

Below is my Code:

Business Rule

When to run: After

Insert: checked

Update: checked

Table: task_ci (CIs Affected)

Script:

(function executeRule(current, previous /*null when async*/ ) {
    var cilist = [];
    if (current.task != '') {
        var grt1 = new GlideRecord('task_ci');
        grt1.addQuery('task', current.task);
        grt1.query();
        gs.addInfoMessage('Total CIs having same Incident: ' + grt1.getRowCount());
        while (grt1.next()) {
            cilist.push(grt1.ci_item.getDisplayValue());
        }
        if (cilist.length > 0) {
            var gr1 = new GlideRecord('incident');
            gr1.addQuery('sys_id', current.task);
            gr1.query();
            if (gr1.next()) {
                gs.addInfoMessage("Incident No: " + gr1.number);
                gr1.u_list_of_associated_cis = "Total Counts: " + grt1.getRowCount() + '\n' + cilist.join(', ');
                gr1.update();
            }
        }
    } else {
        var id = previous.task;
        var grt2 = new GlideRecord('task_ci');
        grt2.addQuery('task', id);
        grt2.query();
        gs.addInfoMessage('Now After removal, Total CIs having the same incident: ' + grt2.getRowCount());
        while (grt2.next()) {
            cilist.push(grt2.ci_item.getDisplayValue());
        }
        if (cilist.length > 0) {
            var gr2 = new GlideRecord('incident');
            gr2.addQuery('sys_id', id);
            gr2.query();
            if (gr2.next()) {
                gr2.u_list_of_associated_cis = "Total Counts: " + grt2.getRowCount() + '\n' + cilist.join(', ');
                gr2.update();
            }
        }
    }
})(current, previous);
 
Attached some snaps of my implementation for your reference.
5 REPLIES 5

Runjay Patel
Giga Sage

Hi @MihirG ,

 

Just create one business rule and use below script. It will work for all the scenarios.

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

    var taskCount = new GlideAggregate('task_ci');
    taskCount.addQuery('task', current.task);
    taskCount.addAggregate('COUNT');
    taskCount.query();
    if (taskCount.next()) {
        var gr2 = new GlideRecord('incident');
        gr2.get(current.task);
        gr2.u_list_of_associated_cis = "Total Counts: " + taskCount.getAggregate('COUNT');
        gr2.update();

    }

})(current, previous);

 

RunjayPatel_0-1735472331970.png

 

 

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

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

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