Remove duplicates of u_revision_summary field entries from Activity Stream

Michael Galang
Tera Contributor

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 

sys_journal_field, sys_audit and sys_history_line tables. I would like to ask your expert opinion on how I can fix it and any better way to achieve the functionality of the UI action. I would highly appreciate any help. 

Form.JPG


Here are the UI action script and the Script includes: 

UI Action Script:

MichaelGalang_0-1716967008117.png

 

function runClientCode() {
    // Get the current record's sys ID
    var ga = new GlideAjax('global.checkDuplicateSummary');
    ga.addParam('sysparm_name', 'deleteDuplicate');
    ga.addParam('sys_id', g_form.getUniqueValue());
    ga.getXML(checkAnswer);
}

function deleteDuplicatesInAudit() {
    var ga1 = new GlideAjax('checkDuplicateRevisionSummary');
    ga1.addParam('sysparm_name', 'deleteDuplicatesInAudit');
    ga1.addParam('sys_id', g_form.getUniqueValue());
    ga1.getXML(checkAnswer);
}

function deleteDuplicatesInHistoryLine() {
    var ga2 = new GlideAjax('checkDuplicateRevisionSummary');
    ga2.addParam('sysparm_name', 'deleteDuplicatesInHistoryLine');
    ga2.addParam('sys_id', g_form.getUniqueValue());
    ga2.getXML(checkAnswer);
}

function checkAnswer(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    location.reload();
}

function myAction() {
    // Definition of the callback function for OK button
    var myActionCallbackOK = function() {
        runClientCode();
        deleteDuplicatesInAudit();
        deleteDuplicatesInHistoryLine();
    };
    // Definition of the callback function for Cancel button
    var myActionCallbackCancel = function() {
        cancelNow();
    };
    // Dialog window setup
    var dlgBody = 'Are you sure you want to delete the Duplicate Revision Summary log entry?';
    var dlgClass = typeof GlideModal != 'undefined' ? GlideModal : GlideDialogWindow;
    // IMPORTANT define which UI page will be displayed, UI page name OOTB or custom
    var dlg = new dlgClass('glide_confirm_standard');
    dlg.setTitle('Proceed?');
    dlg.setPreference('warning', true);
    dlg.setPreference('title', dlgBody);
    // IMPORTANT define callback properties, properties names
    // are defined in UI page HTML code, use javascript bind() method
    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();
}

Script Includes: 

1. Script Include: checkDuplicateSummary(for sys_journal_field)

MichaelGalang_1-1716967354822.png

 

var checkDuplicateSummary = Class.create();
checkDuplicateSummary.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    checkDuplicate: function(sysId) {
        var journalFieldGR = new GlideRecord('sys_journal_field');
        journalFieldGR.addQuery('element_id', sysId);
        journalFieldGR.orderBy('sys_created_on');
        journalFieldGR.query();
        // Created an object to store unique values
        var uniqueEntries = {};
        // Iterate through each record
        while (journalFieldGR.next()) {
            // Construct a unique key based on value and element_id.
            var key = journalFieldGR.value.toString() + '_' + journalFieldGR.element_id.toString();
            // Check if the key already exists in the uniqueEntries object
            if (!uniqueEntries[key]) {
                uniqueEntries[key] = true;
            } else {
                // If a duplicate is found, return the response
                return 'duplicate found';
            }
        }
        // If no duplicates are found, return a response indicating so
        return 'no duplicate found';
    },
   
    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();
        // Created an object to store unique values
        var uniqueEntries = {};
        // Iterate through each record
        while (journalFieldGR.next()) {
            // Construct a unique key based on value and element_id
            var key = journalFieldGR.value.toString() + '_' + journalFieldGR.element_id.toString();
            // Check if the key already exists in the uniqueEntries object
            if (!uniqueEntries[key]) {
                uniqueEntries[key] = true;
            } else {
                journalFieldGR.deleteRecord();

            }
        }
        // If no duplicates are found, return a response indicating so.
        return 'duplicate deleted';
    }
});


2. Script Include: checkDuplicateRevisionSummary(sys_audit and sys_history_line tables)

var checkDuplicateRevisionSummary = Class.create();
checkDuplicateRevisionSummary.prototype = {
    initialize: function() {},

    checkDuplicatesInAudit: function() {
        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 {
                return 'duplicate found in sys_audit';
            }
        }
        return 'no duplicate found in sys_audit';
    },

    checkDuplicatesInHistoryLine: function() {
        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 {
                return 'duplicate found in sys_history_line';
            }
        }
        return 'no duplicate found in sys_history_line';
    },

    deleteDuplicatesInAudit: function(sysId) {
        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(sysId) {
        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'
};




2 REPLIES 2

Mark Manders
Mega Patron

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

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?