Scripting Help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
Requirement :2 new fields: Affected CIs & Impacted Services on the change_request table. These new fields will be integer field types that are populated with the number of Affected CIs and Impacted Services shown in the related list. THese fields need to be populated by a business rule that checks those related lists and inputs the number associated to the change. When one or more of these Affected CIs are removed or added, the business rule should recalculate the value in these new fields.
Can someone please help me with the Business rule or Script include for populating those filed auto?
I have created the fields however I am stuck with coding Part .
Thanks for your help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
so what did you start with and where are you stuck?
this is an easy requirement
You can have after insert BR on task_ci and task_cmdb_ci_service and use below condition in both the BRs
BR Condition: current.task.sys_class_name == 'change_request' && current.task != ''
(function executeRule(current, previous /* null when async */) {
var changeSysId = '';
if (current.task)
changeSysId = current.getValue('task');
if (previous && previous.task && !changeSysId)
changeSysId = previous.getValue('task');
if (!changeSysId)
return;
var chg = new GlideRecord('change_request');
if (!chg.get(changeSysId))
return;
new ChangeCountUtil().updateAffectedCICount(changeSysId);
})(current, previous);
Then have common reusable script include like this
var ChangeCountUtil = Class.create();
ChangeCountUtil.prototype = {
initialize: function() {},
updateAffectedCICount: function(changeSysId) {
if (!changeSysId)
return;
var count = 0;
var agg = new GlideAggregate('task_ci');
agg.addQuery('task', changeSysId);
agg.addAggregate('COUNT');
agg.query();
if (agg.next())
count = parseInt(agg.getAggregate('COUNT'), 10) || 0;
var chg = new GlideRecord('change_request');
if (chg.get(changeSysId)) {
chg.setValue('u_affected_ci_count', count);
chg.setWorkflow(false);
chg.autoSysFields(false);
chg.update();
}
},
updateImpactedServiceCount: function(changeSysId) {
if (!changeSysId)
return;
var count = 0;
// Adjust this table/query if your impacted services are stored elsewhere
var agg = new GlideAggregate('task_cmdb_ci_service');
agg.addQuery('task', changeSysId); // verify field name in your instance
agg.addAggregate('COUNT');
agg.query();
if (agg.next())
count = parseInt(agg.getAggregate('COUNT'), 10) || 0;
var chg = new GlideRecord('change_request');
if (chg.get(changeSysId)) {
chg.setValue('u_impacted_service_count', count);
chg.setWorkflow(false);
chg.autoSysFields(false);
chg.update();
}
},
type: 'ChangeCountUtil'
};
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @pramn .
If you have 2 integer fields - u_affected_cis changes & u_impacted_services
try with this:
- Navigate to System Definition > Business Rules>Click New.
- Fill in the following details:
- Name: Calculate Impact and CIs
- Table: change_request
- Advanced: Checked
- When: After
- Insert & Update: Checked
- Condition: Check u_affected_cis changes OR u_impacted_services changes
Sample code:
(function executeRule(current, previous /*null when async*/) {
var affectedCnt = 0;
var affectedCI = new GlideRecord('task_ci');
affectedCI.addQuery('task', current.sys_id);
affectedCI.query();
affectedCnt = affectedCI.getRowCount();
var impactedCnt = 0;
var impactedService = new GlideRecord('task_cmdb_ci_service');
impactedService.addQuery('task', current.sys_id);
impactedService.query();
impactedCnt = impactedService.getRowCount();
var changeGR = new GlideRecord('change_request');
if (changeGR.get(current.sys_id)) {
changeGR.setWorkflow(false);
changeGR.u_affected_cis = affectedCnt;
changeGR.u_impacted_services = impactedCnt;
changeGR.update();
}
})(current, previous);
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti