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

Ankur Bawiskar
Tera Patron
Tera Patron

@MihirG 

you should have 1 more BR which is before delete on task_ci and reduce the count and update the CIs

Script:

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

    var id = current.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);

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@MihirG 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Nilesh Wahule
Tera Guru

Hi @MihirG ,

Your existing BR is working on the insert and update, but the delete operation is missing when affected CI's are deleted from the incident.

 

Hence create a new delete business rule with below script

 

Business Rule

When to run: Before

Delete: checked

Table: task_ci (CIs Affected)

Script

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

    // Add your code here


    var grIncident = new GlideRecord('incident');
    if (grIncident.get(current.task)) {
		var ciResult = getAffectedCI(current.task);
		var details = "Total Count = "+ ciResult.count + "\n";
		details += ciResult.cis;
		grIncident.u_list_of_associated_cis = details; // check the name of the field
		grIncident.update();
		gs.addInfoMessage("Affected CI's are updated for incident "+ grIncident.number);
    }


    function getAffectedCI(incidentSysId) {
        var grAffectedCI = new GlideRecord('task_ci');
        grAffectedCI.addQuery('task', incidentSysId);
        grAffectedCI.query();
        var count = grAffectedCI.getRowCount();
        var affected_ci = '';
        while (grAffectedCI.next()) {
            affected_ci += grAffectedCI.getDisplayValue() + ',';
        }
		return {
			count: count,
			cis: affected_ci
		};
    }

})(current, previous);

 

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

Please mark my answer as helpful/correct if it resolves your query.

Thanks,
Nilesh Wahule

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



 

rambo1
Tera Guru

@MihirG 

Add 2 business rules one after insert and one before delete

Both on task_ci table 

Below are sample codes:

BR after insert:

(function executeRule(current, previous /*null when async*/) {
if (current.task.getRefRecord().getTableName() === 'incident') {
updateAssociatedCIs(current.task.sys_id);
}
})(current, previous);

function updateAssociatedCIs(taskSysId) {
var incidentGR = new GlideRecord('incident');
if (incidentGR.get(taskSysId)) {
var ciList = getAssociatedCIs(taskSysId);
var ciCount = ciList.length;
var ciListString = ciList.join(', ');

incidentGR.u_list_of_associated_cis = 'Count: ' + ciCount + ', CIs: ' + ciListString;
incidentGR.update();
}
}

function getAssociatedCIs(taskSysId) {
var ciRelGR = new GlideRecord('task_ci');
ciRelGR.addQuery('task', taskSysId);
ciRelGR.query();

var ciList = [];
while (ciRelGR.next()) {
ciList.push(ciRelGR.ci_item.getDisplayValue());
}
return ciList;
}

BR before delete:

(function executeRule(current, previous /*null when async*/) {
if (current.task.getRefRecord().getTableName() === 'incident') {
updateAssociatedCIs(current.task.sys_id);
}
})(current, previous);

function updateAssociatedCIs(taskSysId) {
var incidentGR = new GlideRecord('incident');
if (incidentGR.get(taskSysId)) {
var ciList = getAssociatedCIs(taskSysId);
var ciCount = ciList.length;
var ciListString = ciList.join(', ');

incidentGR.u_list_of_associated_cis = 'Count: ' + ciCount + ', CIs: ' + ciListString;
incidentGR.update();
}
}

function getAssociatedCIs(taskSysId) {
var ciRelGR = new GlideRecord('task_ci');
ciRelGR.addQuery('task', taskSysId);
ciRelGR.query();

var ciList = [];
while (ciRelGR.next()) {
ciList.push(ciRelGR.ci_item.getDisplayValue());
}
return ciList;
}