need to auto populate impacted services from affected cis in the related list
						
					
					
				
			
		
	
			
	
	
	
	
	
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
11-04-2019 08:03 PM
I need to auto-populate impacted services from affected cis in the related list.
I have written a display BR below which is not populating services, Can you tell me what's missing?
function onDisplay(current, g_scratchpad) {
 removeAffectedServices(current);
 if (!current.cmdb_ci.nil()) {
 addAffectedServices(current);
 }
}
function removeAffectedServices(current) {
 var m2m = new GlideRecord('task_cmdb_ci_service');
 m2m.addQuery('task',current.sys_id);
 m2m.addQuery('manually_added','false');
 m2m.query();
 m2m.deleteMultiple();
}
function addAffectedServices(current) {
 var affectedItem = new GlideRecord('task_ci');
 affectedItem.addQuery('task', current.sys_id);
 affectedItem.query();
 var aff_ci;
 while (affectedItem.next()) {
 aff_ci = affectedItem.ci_item.toString();
 var ciu = new CIUtils();
 var services = ciu.servicesAffectedByCI(aff_ci);
 var m2m = new GlideRecord('task_cmdb_ci_service');
 for (var i = 0; i < services.length; i++) {
 m2m.initialize();
 m2m.task = current.sys_id;
 m2m.cmdb_ci_service = services[i];
 m2m.manually_added = 'false';
 m2m.insert();
 }
 }
}
- Labels:
 - 
						
							
		
			Change Management
 
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
11-04-2019 08:15 PM
I think this has to be a after insert /delete BR on affected CI's table
(function executeRule(current, previous /*null when async*/) {
if(current.operation()=='insert'){
	var impacted_ci = new GlideRecord('task_cmdb_ci_service');
	impacted_ci.initialize();
	impacted_ci.task = current.task;
	impacted_ci.cmdb_ci_service = current.ci_item;
	impacted_ci.insert();
}
	else{
		var impacted_ci_del = new GlideRecord('task_cmdb_ci_service');
	impacted_ci_del.addQuery('task',current.task);
	impacted_ci_del.addQuery('cmdb_ci_service',current.ci_item);
	impacted_ci_del.query();
	while(impacted_ci_del.next()){
	impacted_ci_del.delete();
	}
	}
})(current, previous);
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
11-04-2019 09:01 PM
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
11-04-2019 09:04 PM
Hi,
you need to populate the records in that related list table based on the affected cis
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
11-04-2019 09:19 PM
