Scripting

kali
Tera Contributor

Hi All,

There is a custom table , there are two fields called code and name  , I need to run a schedule job to check if there are more than one record with same code  then the schedule job should make the record as inactive which has the least updated time of the records . Please help me on this.

Thanks in advance

2 REPLIES 2

Pratima Kalamka
Kilo Sage

Hello @kali ,

try below script in schedule job:

// Get the custom table's GlideRecord
var customTableGR = new GlideRecord('your_custom_table');
// Add a query to filter active records
customTableGR.addQuery('active', true);
// Group by the 'code' field to identify duplicates
customTableGR.groupBy('code');
// For each group of duplicate records
while (customTableGR.next()) {
// If there's more than one record in the group
    if (customTableGR.getRowCount() > 1) {
        // Sort the records by updated time in ascending order
        customTableGR.orderBy('sys_updated_on');
        
        // Get the first (oldest) record
        var oldestRecord = customTableGR.next();
        
        // Deactivate all other records in the group
        while (customTableGR.next()) {
            customTableGR.active = false;
            customTableGR.update();
        }
        
        // Log which records were deactivated
        gs.info('Deactivated duplicate records for code: ' + oldestRecord.code);
    }
}

 

If my answer is helpful please mark it as helpful or correct!!

 

Pratima.k

Hi @Pratima Kalamka ,

Instead of glide record can we use glide aggregate as it is best practices. Please help me in scripting to inactive more than 2 records under the same name code. Thanks for your previous response