How to inactive the duplicate records?

Nithya Devi
Tera Contributor

How to write the script to inactive the duplicate records on risk assessment table? Due to the accidental import of risk assessment data from UAT to PROD the duplicates found on production env. where number is the unique field on the risk assessment table. I want the make the original one as true and duplicate to be false. Could anyone suggest script to fix the duplicates without impacting other original records?

1 ACCEPTED SOLUTION

Shashank_Jain
Kilo Sage

@Nithya Devi , Try this script and tell me if this works.

 

var ga = new GlideAggregate('risk_assessment');
ga.addAggregate('COUNT', 'number');
ga.groupBy('number');
ga.addHaving('COUNT', '>', 1);  // only get duplicates
ga.query();
while (ga.next()) {
    var dupNumber = ga.getValue('number');

    var gr = new GlideRecord('risk_assessment');
    gr.addQuery('number', dupNumber);
    gr.orderBy('sys_created_on'); // oldest (original) will come first
    gr.query();

    var isFirst = true;
    while (gr.next()) {
        if (isFirst) {
            // Keep the first/original active
            gr.active = true;
            gr.update();
            isFirst = false;
        } else {
            // Mark duplicates inactive
            gr.active = false;
            gr.update();
        }
    }
}
gs.print("Duplicate cleanup complete");

 

Hope it helps!

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

View solution in original post

13 REPLIES 13

Hi Bhuvan,

 

I suggest making them inactive. Not to delete. Because if the delete the records it will delete the associated child records which will impact the original record.

 

Regards,

Nithya.

@Nithya Devi 

 

Yes, you can query and get the row count and update the records and set it to inactive using the background scripts I provided. 

 

If this helped to answer your query, please mark the posts helpful & accept the solution.

 

Thanks,

Bhuvan

@Nithya Devi 

 

Did you get a chance to review this ?

 

If my response helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Hi Bhuvan,

 

Thanks for the immediate response.

I will review on this and update you asap.

 

Regards,

Nithya.

@Nithya Devi 

 

You can simulate the scenario in PDI and try it. Once you test and it works fine, please mark the posts helpful and accept the solution.

 

If you need more help or something is not working, please share the details and can help to resolve it.

 

Thanks,

Bhuvan