Not applicable

Hi @kali ,

I've tried to deal with your requirement using this below script, give it a try. I've added appropriate comments as well.

// Step 1: Use GlideAggregate to get unique codes
var ga = new GlideAggregate('your_custom_table');
ga.addAggregate('COUNT', 'code');
ga.groupBy('code');
ga.query();
 
while (ga.next()) {
    var code = ga.getValue('code');
 
    // Step 2: Use GlideRecord to find records with this code and sort by updated date
    var gr = new GlideRecord('your_custom_table');
    gr.addQuery('code', code);
    gr.orderByDesc('sys_updated_on'); // Sort by updated date descending
    gr.query();
 
    // Step 3: Loop through the records and update the active field
    var firstRecord = true; // Flag to keep track of the most recent record
while (gr.next()) {
        if (firstRecord) {
            gr.setValue('active', true); // Keep the most recent record active
            firstRecord = false; // Set flag to false after updating the first record
        } else {
            gr.setValue('active', false); // Set all other records to inactive
        }
        gr.update(); // Save the record
    }
}

 

Thanks,

Hope this helps.

If my response proves helpful please mark it helpful and accept it as solution to close this thread.

 

View solution in original post