How to delete duplicate catalog item draft
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago - last edited 3 hours ago
Hi all,
I need to delete duplicate catalog item draft. I need to only keep last created. There is one question 'Jira ID' where I am getting unique value from Jira, Based on that question's value I have do the glide aggregate count. If the count is greater than 1 (means there are more than 1 record where Jira Id is same) then I will only keep the latest one.
How can I write this glide aggregate code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
what's your actual business requirement?
when should the drafts for that catalog item be deleted?
what's the trigger?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @Obito ,
Please try with the below script to delete the Duplicate Catalog items
(function() {
var ga = new GlideAggregate('sc_cart_item'); // or your catalog item table
ga.addAggregate('COUNT');
ga.groupBy('u_jira_id'); // Replace with the actual question variable name or field
ga.query();
while (ga.next()) {
var jiraId = ga.getValue('u_jira_id');
var count = parseInt(ga.getAggregate('COUNT'));
if (count > 1) {
// Get all records with this Jira ID, sorted by creation date descending
var gr = new GlideRecord('sc_cart_item');
gr.addQuery('u_jira_id', jiraId);
gr.orderByDesc('sys_created_on');
gr.query();
var keepFirst = true;
while (gr.next()) {
if (keepFirst) {
keepFirst = false; // Keep the latest one
continue;
}
// Delete older duplicates
gr.deleteRecord();
}
}
}
})();
If the provided solution is helpful to you, please mark it as "Helpful" and "Accept" the solution.
Regards,
Raviteja