- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-17-2024 03:48 AM
Hi All,
I have a custom table and there are two fields called code and name . There are duplicate entries in the table. Under code there are 3 records . I ran a glide aggregate and got the records and using gliderecord i need to update the active as false for 2 records. Records which are updated recently will be kept as true and others will be made false. How to write the condition such that it leave the recently updated records and make other records as false.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-17-2024 04:20 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-17-2024 04:20 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-17-2024 06:47 AM
Hi @HrishabhKumar ,
Thank for your help