Delete duplicate attachment script not working

Maddysunil
Kilo Sage

Hi Everyone, I have written a script action in human resource:core application where i am trying to delete duplicate attachment from case form, i am getting all the logs still attachment is not getting deleted.

var caseSysID = current.parent.toString();
gs.info("caseSysIDLog " + caseSysID); // getting this log 
var taskAttachGr = new GlideRecord('sys_attachment');
taskAttachGr.addQuery('table_sys_id', current.sys_id.toString());
taskAttachGr.query();
while (taskAttachGr.next()) {
    var taskFile = taskAttachGr.file_name;
    gs.info("taskFileLog " + taskFile); // getting this log 

    var attach_count;
    var attachGa = new GlideAggregate('sys_attachment');

    attachGa.addQuery('table_sys_id', caseSysID.toString());
    attachGa.addQuery('file_name', taskFile.toString());
    attachGa.addAggregate('COUNT');
    attachGa.query();

    gs.info('Entered Script Include duplicate attachment Row Count' + "hello"); // getting this log 
    if (attachGa.next() && attachGa.getAggregate('COUNT') != 1) {
        attach_count = attachGa.getAggregate('COUNT');
        gs.info('Entered Script Include duplicate attachment Aggregate Count' + attach_count);// getting this log 
        var attachGr = new GlideRecord('sys_attachment');
        attachGr.addQuery('table_sys_id', caseSysID.toString());
        attachGr.addQuery('file_name', taskFile.toString());
        attachGr.query();
        if (attachGr.next()) {
            gs.info('Delete Record duplicate attachment'); // getting this log
            attachGr.deleteRecord();
            attachGr.update();
        }
    }

}

Below is the business rule from where i am calling the script action after 5 seconds

var caseSysID = current.parent;

    var caseGr = new GlideRecord('sn_hr_core_case_talent_management');
    caseGr.addQuery('sys_id', caseSysID.toString());
    caseGr.query();
    if (caseGr.next()) {
        var hrService = caseGr.hr_service;
    }

    var complianceReview = gs.getProperty('sn_hr_core.compliance_review');

    if (hrService == complianceReview) { //Delete duplicate attachments only for compliance 

        var wait5sec = new GlideDateTime();
        wait5sec.addSeconds(5); // 5 sec in the future.
        gs.eventQueueScheduled('sn_hr_core.duplicate.attachment.on.case', current, '', '', wait5sec);
}

Any help will be appreciated ..Thanks

5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Remove the 

attachGr.update();

after

attachGr.deleteRecord();

Hi @Maik Skoddow  I tried without attachGr.update() still not working

Sandeep Rajput
Tera Patron
Tera Patron

@Maddysunil Try to put your code responsible for deletion of attachment inside a script include in the Global scope and call the same script include from your script action in HR Scope. I faced the similar issue in the past and I managed to address the issue by putting the deleteRecord operation the the Global scope.

 

Hope this helps.

@Sandeep Rajput 

I tried with that still it was not able to delete..So i tried with an alternative approach and its working, I wrote before insert BR on attachment table itself and for duplicate attachment i aborted the action, below is the working code:

if (current.table_name == 'sn_hr_core_case_talent_management') {
        var caseSysID = current.table_sys_id.toString();
        var caseGr = new GlideRecord('sn_hr_core_case_talent_management');
        caseGr.addQuery('sys_id', caseSysID);
        caseGr.query();
        if (caseGr.next()) {
            var complianceReview = gs.getProperty('sn_hr_core.compliance_review');
            if (caseGr.hr_service == complianceReview) {
                var attachGr = new GlideRecord('sys_attachment');
                attachGr.addQuery('table_sys_id', caseSysID);
                attachGr.addQuery('file_name', current.file_name.toString());
				attachGr.query();
                if (attachGr.next()) {
                    current.setAbortAction(true);
                }
            }
        }
    }