Add values coming from different tickets to a list collector without removing old ones
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 06:36 AM
Hello,
I am having a list collector field on a parent ticket which will get change requests associated with different child tickets related to the parent ticket. sometime same change number is associated with different child tickets and so i want to restrict the adding of same change number multiple times. however i can restrict the duplicate ones, but its also removing the existing tickets added to parent ticket and adding the values coming from child tickets. How can i add the change values coming from different child tickets without adding duplicate and without removing old tickets.
I am using script as follows:
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 06:48 AM
hello @Kumar147
Something with below code might help you.
var arr = [];
var grParent = new GlideRecord('Parent_table');
if (grParent.get(current.parent)) {
// Get existing change requests from the parent ticket
var existingChanges = grParent.u_dependent_change.split(',');
arr = arr.concat(existingChanges);
}
var gr = new GlideRecord('child_table');
gr.addQuery('parent.sys_id', current.parent);
gr.query();
while (gr.next()) {
arr.push(gr.getValue('u_related_change'));
}
// Remove duplicates
var arrayUtil = new ArrayUtil();
var uniqueArr = arrayUtil.unique(arr);
// Update the parent ticket with the unique change requests
grParent.u_dependent_change = uniqueArr.join(',');
grParent.update();
Script is doing below stuff:
- Retrieves the existing change requests from the parent ticket and adds them to the arr array.
- Adds the change requests from the child tickets to the arr array.
- Uses the ArrayUtil class to remove duplicates from the arr array.
- Updates the parent ticket with the unique change requests.
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 07:51 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 07:26 AM
try this
var arr = [];
var parentSysId = current.parent.sys_id;
// Retrieve existing change values from the parent ticket
var parentGr = new GlideRecord('Parent_table');
if (parentGr.get(parentSysId)) {
var existingChanges = parentGr.u_dependent_change.split(',');
arr = arr.concat(existingChanges);
}
// Retrieve change values from child tickets
var gr = new GlideRecord('child_table');
gr.addQuery('parent.sys_id', parentSysId);
gr.query();
while (gr.next()) {
var relatedChange = gr.getValue('u_related_change');
if (relatedChange && !arr.includes(relatedChange)) {
arr.push(relatedChange);
}
}
// Update the parent ticket with the combined unique change values
parentGr.u_dependent_change = arr.join(',');
parentGr.update();
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-17-2025 07:52 AM