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

wenzhenz
Tera Contributor

@Maddysunil 

I tried below code,and it works in global scope, but failed in other scopes。
※Delete operation against 'sys_attachment' from scope 'x_615438_testx' has been refused due to the table's cross-scope access policy

Open the table's configration page and check on the "Can delete"  in "Application Access " tab.

 

 

 

 

   var attachGa = new GlideAggregate('sys_attachment');
   attachGa.addQuery('table_sys_id', '47064b68a9fe19810186793eefffc9b7');
   attachGa.addAggregate('COUNT');  //not necessary
   attachGa.addAggregate('GROUP_CONCAT', 'sys_id');
   attachGa.groupBy('file_name');
   attachGa.query();

   while (attachGa.next()) {
       var count = attachGa.getAggregate('COUNT');  //not necessary
       gs.info("file_name " + attachGa.file_name + " count:" + count)

       if (count >= 2) {       //not necessary
           var sys_ids = attachGa.getAggregate('GROUP_CONCAT', 'sys_id');
           var sys_idsList = sys_ids.split(",");
           for (var i = 1; i < sys_idsList.length; i++) {    //skip the first one
               var gr = new GlideRecord('sys_attachment');
               gr.get(sys_idsList[i]);
               gr.deleteRecord();
               //gs.info(gr.sys_id);
           }

       }
   }

 

 

 

wenzhenz_0-1717387992958.png