Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Replace the duplicate number

VaibhavJ
Tera Contributor

Hi All,

 

I am trying to replace the duplicate number in the table and write down the below script 

var dupRecords = [];

    var gaDupCheck1 = new GlideAggregate('cmdb_ci_business_app');

    gaDupCheck1.addQuery('active','true');

    gaDupCheck1.addAggregate('COUNT', 'number');

    gaDupCheck1.groupBy('number');

    gaDupCheck1.addHaving('COUNT', '>', 1);

    gaDupCheck1.query();

    while (gaDupCheck1.next()) {

          dupRecords.push(gaDupCheck1.number.toString());

    }

    gs.print(dupRecords);

fixDuplicates(dupRecords);

    function fixDuplicates(dupRecords) {

   var gr = new GlideRecord('cmdb_ci_business_app');

   gr.addQuery('number', dupRecords);

   gr.orderByDesc('sys_created_on');

   gr.query();

while (gr.next()) {

var nm = new NumberManager('cmdb_ci_business_app');

gr.number = nm.getNextObjNumberPadded();

//gr.update();

}

gs.print(gr.number);

}

 

But when i executed this script, it will replace both the duplicate number with the next available counter where I just need to update only one duplicate number record.

Can someone check this and provide the solution?

 

Thanks!

Vaibhav J

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@VaibhavJ 

what debugging have you done so far?

few changes

gr.addQuery('number', 'IN' , dupRecords);

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

Hi Ankur,

 

This solution is not working and still changing both the numbers.

 

Thanks!

Vaibhav J