How to prevent adding duplicates in list collector

Kumar147
Tera Contributor

Hi,

A list collector field is added to Parent and child tickets.

Related tickets from multiple sub tasks are getting added to parent. When two or more sub tasks having same related ticket it is getting duplicated and adding multiple times in parent related task field. How to restrict adding of ticket if that ticket already added to parent related ticket list field. 

Thanks in advance

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Kumar147 

just before adding the values use arrayUtil unique method to remove duplicates and then set the field value

ArrayUtil - Global 

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

View solution in original post

5 REPLIES 5

Hello @Ankur Bawiskar 

I can restrict the adding of duplicate values. but It also removing the existing values in the field and adding only the values that are coming from the target fields. how to add the target fields data without removing the existing one. I am using the following script.

 

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();
        }
    }