Remove duplicates of u_revision_summary field entries from Activity Stream
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 12:29 AM
Hi Experts,
I created a UI action button 'Delete Duplicates Revision Summary' found in a record form. The functionality is to remove the duplicates entries of u_revision_summary field from the Activity Stream. Please refer to below image. However, it is not working based on the expected output. Everytime I click it, it it didn't remove the duplicates from the Activity stream.
Please see below UI Script action and the two Script includes to check and delete the duplicates from the
Here are the UI action script and the Script includes:
UI Action Script:
Script Includes:
1. Script Include: checkDuplicateSummary(for sys_journal_field)
2. Script Include: checkDuplicateRevisionSummary(sys_audit and sys_history_line tables)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 01:05 AM
Is the 'sys_id' correctly passed?
function runClientCode() {
// Get the current record's sys ID
var ga = new GlideAjax('checkDuplicateSummary'); // Corrected class name
ga.addParam('sysparm_name', 'deleteDuplicate');
ga.addParam('sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(checkAnswer);
}
function deleteDuplicatesInAudit() {
var ga1 = new GlideAjax('checkDuplicateRevisionSummary'); // Corrected class name
ga1.addParam('sysparm_name', 'deleteDuplicatesInAudit');
ga1.addParam('sys_id', g_form.getUniqueValue());
ga1.getXMLAnswer(checkAnswer);
}
function deleteDuplicatesInHistoryLine() {
var ga2 = new GlideAjax('checkDuplicateRevisionSummary');
ga2.addParam('sysparm_name', 'deleteDuplicatesInHistoryLine');
ga2.addParam('sys_id', g_form.getUniqueValue());
ga2.getXMLAnswer(checkAnswer);
}
function checkAnswer(response) {
var answer = response;
if (answer === 'duplicate deleted') {
location.reload();
} else {
alert('No duplicates found or deletion failed.');
}
}
function myAction() {
var myActionCallbackOK = function() {
runClientCode();
deleteDuplicatesInAudit();
deleteDuplicatesInHistoryLine();
};
var myActionCallbackCancel = function() {
cancelNow();
};
var dlgBody = 'Are you sure you want to delete the Duplicate Revision Summary log entry?';
var dlgClass = typeof GlideModal != 'undefined' ? GlideModal : GlideDialogWindow;
var dlg = new dlgClass('glide_confirm_standard');
dlg.setTitle('Proceed?');
dlg.setPreference('warning', true);
dlg.setPreference('title', dlgBody);
dlg.setPreference('onPromptComplete', myActionCallbackOK.bind(this));
dlg.setPreference('onPromptCancel', myActionCallbackCancel.bind(this));
dlg.render();
}
function cancelNow() {
var dlgClass = typeof GlideModal != 'undefined' ? GlideModal : GlideDialogWindow;
var dlg = new dlgClass('glide_confirm_standard');
dlg.destroy();
}
var checkDuplicateSummary = Class.create();
checkDuplicateSummary.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
deleteDuplicate: function() {
var sysId = this.getParameter('sys_id');
var journalFieldGR = new GlideRecord('sys_journal_field');
journalFieldGR.addQuery('element_id', sysId);
journalFieldGR.orderBy('sys_created_on');
journalFieldGR.query();
var uniqueEntries = {};
while (journalFieldGR.next()) {
var key = journalFieldGR.value.toString() + '_' + journalFieldGR.element_id.toString();
if (!uniqueEntries[key]) {
uniqueEntries[key] = true;
} else {
journalFieldGR.deleteRecord();
}
}
return 'duplicate deleted';
},
type: 'checkDuplicateSummary'
});
var checkDuplicateRevisionSummary = Class.create();
checkDuplicateRevisionSummary.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
deleteDuplicatesInAudit: function() {
var sysId = this.getParameter('sys_id');
var auditGR = new GlideRecord('sys_audit');
auditGR.addQuery('documentkey', sysId);
auditGR.orderBy('sys_created_on');
auditGR.query();
var uniqueAuditEntries = {};
while (auditGR.next()) {
var auditKey = auditGR.newvalue.toString() + '_' + auditGR.documentkey.toString();
if (!uniqueAuditEntries[auditKey]) {
uniqueAuditEntries[auditKey] = true;
} else {
auditGR.deleteRecord();
}
}
return 'duplicate deleted in sys_audit';
},
deleteDuplicatesInHistoryLine: function() {
var sysId = this.getParameter('sys_id');
var historyLineGR = new GlideRecord('sys_history_line');
historyLineGR.addQuery('set', sysId);
historyLineGR.orderBy('sys_created_on');
historyLineGR.query();
var uniqueHistoryLineEntries = {};
while (historyLineGR.next()) {
var historyLineKey = historyLineGR.value.toString() + '_' + historyLineGR.set.toString();
if (!uniqueHistoryLineEntries[historyLineKey]) {
uniqueHistoryLineEntries[historyLineKey] = true;
} else {
historyLineGR.deleteRecord();
}
}
return 'duplicate deleted in sys_history_line';
},
type: 'checkDuplicateRevisionSummary'
});
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 01:18 AM
Hi Mark,
Thank you for your response. The g_form.getUniqueValue() found in the UI Action script, I believe will get the sys_id of the current record and pass it to the GlideAjax call. Any suggestion or alternatives?