Add values coming from different tickets to a list collector without removing old ones

Kumar147
Tera Contributor

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: 

    var arr = [];
    var gr = new GlideRecord('child_table');
    gr.addQuery('parent.sys_id', current.parent);
    gr.query();
    while (gr.next()) {
        var gr1 = new GlideRecord('Parent_table');
        gr1.addQuery('sys_id', gr.parent);
        gr1.query();
        while (gr1.next()) {
            arr.push(gr.getValue('u_related_change'));
            var result = arr.join(',');
            var arrnew = result.split(',');
            var arrayUtil = new ArrayUtil();
            var arr1 = arrayUtil.unique(arrnew);
            gr1.u_dependent_change = arr1.join(',');
            gr1.update();
        }
    }

 

Thanks in advance.

9 REPLIES 9

Viraj Hudlikar
Giga Sage

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:

  1. Retrieves the existing change requests from the parent ticket and adds them to the arr array.
  2. Adds the change requests from the child tickets to the arr array.
  3. Uses the ArrayUtil class to remove duplicates from the arr array.
  4. 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.

Hello @Viraj Hudlikar 

Tried above script and values are duplicated.

Kumar147_0-1742222979088.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Kumar147 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar 

Tried but values are getting duplicated

Kumar147_1-1742223046548.png