How to pass values to a list collector from another list collector and remove when it removed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2025 07:02 AM
Hello,
I have two List collectors on different tables.
List collector 1 - Change Table - Referenced to Incident
List collector 2 - Incident Table - Referenced to Change
When List Collector 2 on Incident Table is updated with any Change Request, The Incident number should be added to the List collector 1 on Change Table. similarly When Change request is Removed from List Collector 2 on Incident Table, The Incident number should be removed from List collector 1 on Change Table. List Collector 2 can have multiple changes linked. So when any change request is mapped to multiple Incidents and unmapped from any one of the Incident, the current Incident should be unmapped from the that change request and the remaining incidents should remains added.
Can anyone help me out this thing. Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 12:45 AM
Why do you have the ability to link one Incident to multiple Changes? That doesn't really make sense from a process point of view. And what's wrong with the OOB related lists that just show which Incidents are related to that Change?
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2025 02:51 AM
Hi @PHANI7 , You could write an after business rule in incident with condition - when the particular list field changes(u_change_requests)-
(function executeRule(current, previous /*null when async*/ ) {
try {
// Add your code here
var currChanges = current.getValue('u_change_requests');
var curr = currChanges.split(',');
var sysId = current.getValue('sys_id').toString();
if (current.operation() == 'insert') {
//only adding new records can happen
addition(currChanges, sysId);
} //both adding and removal can happen
else if (current.operation() == 'update') {
var preChanges = previous.getValue('u_change_requests');
var pre = preChanges.split(',');
//compare the length - to see if record is removed or added
//record is added
if (curr.length > pre.length) {
//get the records that were added new
var arrayUtil = new ArrayUtil();
var rec = arrayUtil.diff(curr, pre);
addition(rec, sysId);
}
//record is removed
else if (curr.length < pre.length) {
var arrayUtil = new ArrayUtil();
var rec = arrayUtil.diff(pre, curr);
removal(rec, sysId);
}
//record is added and removed at the same time
else if (curr.length == pre.length) {
var arrayUtil = new ArrayUtil();
var toAdd = arrayUtil.diff(curr, pre);
var returnCall = addition(toAdd, sysId);
var toRemove = arrayUtil.diff(pre, curr);
returnCall = removal(toRemove, sysId);
}
}
} catch (e) {
gs.addInfoMessage(e.message + e.lineNumber);
}
function addition(currChanges, sysId) {
var updChg = new GlideRecord('change_request');
updChg.addQuery('sys_id', 'IN', currChanges);
updChg.query();
while (updChg.next()) {
var existingInc = updChg.getValue('u_incidents');
if (existingInc) {
existingInc = existingInc.split(',');
existingInc.push(sysId);
updChg.u_incidents = existingInc.join(',');
updChg.update();
} else {
updChg.u_incidents = sysId;
updChg.update();
}
}
return true;
}
function removal(currChanges, sysId) {
var updChg = new GlideRecord('change_request');
updChg.addQuery('sys_id', 'IN', currChanges);
updChg.query();
while (updChg.next()) {
var existingInc = updChg.getValue('u_incidents').split(',');
var index = existingInc.indexOf(sysId.toString());
existingInc.splice(index, 1);
updChg.u_incidents = existingInc.join(',');
updChg.update();
}
return true;
}
})(current, previous);
Adjust the code with your fields and test it. And in case of vice versa, you can replicate this code for change_request table also.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2025 02:56 AM
something like this
1) After update BR on incident table, Condition List collector changes
(function executeRule(current, previous /*null when async*/) {
// Avoid recursion
if (gs.getSession().getProperty('skip_update') == 'true') {
gs.getSession().setProperty('skip_update', 'false');
return;
}
// Get the list of changes from the List Collector 2
var currentChanges = current.u_list_collector_2.split(',');
var previousChanges = previous.u_list_collector_2.split(',');
// Find added and removed changes
var addedChanges = currentChanges.filter(function(change) {
return previousChanges.indexOf(change) === -1;
});
var removedChanges = previousChanges.filter(function(change) {
return currentChanges.indexOf(change) === -1;
});
// Add the Incident number to the List Collector 1 on the Change table
addedChanges.forEach(function(changeSysId) {
var changeGR = new GlideRecord('change_request');
if (changeGR.get(changeSysId)) {
var incidentList = changeGR.u_list_collector_1.split(',');
if (incidentList.indexOf(current.sys_id.toString()) === -1) {
incidentList.push(current.sys_id.toString());
changeGR.u_list_collector_1 = incidentList.join(',');
gs.getSession().setProperty('skip_update', 'true');
changeGR.update();
}
}
});
// Remove the Incident number from the List Collector 1 on the Change table
removedChanges.forEach(function(changeSysId) {
var changeGR = new GlideRecord('change_request');
if (changeGR.get(changeSysId)) {
var incidentList = changeGR.u_list_collector_1.split(',');
var index = incidentList.indexOf(current.sys_id.toString());
if (index !== -1) {
incidentList.splice(index, 1);
changeGR.u_list_collector_1 = incidentList.join(',');
gs.getSession().setProperty('skip_update', 'true');
changeGR.update();
}
}
});
})(current, previous);
2) after update on CHG, condition List collector changes
(function executeRule(current, previous /*null when async*/) {
// Avoid recursion
if (gs.getSession().getProperty('skip_update') == 'true') {
gs.getSession().setProperty('skip_update', 'false');
return;
}
// Get the list of incidents from the List Collector 1
var currentIncidents = current.u_list_collector_1.split(',');
var previousIncidents = previous.u_list_collector_1.split(',');
// Find added and removed incidents
var addedIncidents = currentIncidents.filter(function(incident) {
return previousIncidents.indexOf(incident) === -1;
});
var removedIncidents = previousIncidents.filter(function(incident) {
return currentIncidents.indexOf(incident) === -1;
});
// Add the Change Request number to the List Collector 2 on the Incident table
addedIncidents.forEach(function(incidentSysId) {
var incidentGR = new GlideRecord('incident');
if (incidentGR.get(incidentSysId)) {
var changeList = incidentGR.u_list_collector_2.split(',');
if (changeList.indexOf(current.sys_id.toString()) === -1) {
changeList.push(current.sys_id.toString());
incidentGR.u_list_collector_2 = changeList.join(',');
gs.getSession().setProperty('skip_update', 'true');
incidentGR.update();
}
}
});
// Remove the Change Request number from the List Collector 2 on the Incident table
removedIncidents.forEach(function(incidentSysId) {
var incidentGR = new GlideRecord('incident');
if (incidentGR.get(incidentSysId)) {
var changeList = incidentGR.u_list_collector_2.split(',');
var index = changeList.indexOf(current.sys_id.toString());
if (index !== -1) {
changeList.splice(index, 1);
incidentGR.u_list_collector_2 = changeList.join(',');
gs.getSession().setProperty('skip_update', 'true');
incidentGR.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
03-23-2025 09:33 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