Assistance Required for Updating Custom Field without refresh in Incident Record via Business Rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2024 08:15 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2024 09:05 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2024 06:43 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2024 10:04 PM
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
---------------------------------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2024 10:08 PM
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;
}