Scripting Help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 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
an hour 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