Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scheduled Data Import Post Script Can't Delete Attachment

CMartinTN
Tera Expert
I have followed the code for several solutions provided in these forums without success.
 
I am running this from a scoped app but I have checked the Can Delete checkbox on the Attachment table for All Application Scopes.
 
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', '');  //The sys_id is between the quotes but redacted
gr.query();
while(gr.next()){
    gs.info('Found attachment');  //This one does print
    gs.info('Can Delete Attachment :' + gr.canDelete()); //This one gives me true
    gs.info('File Name to be Deleted ' + gr.file_name); //This one gives me the proper file name
    gr.deleteRecord(); //This one does not work and actually returns a value of false
}
 
I have also tried gr.deleteMultiple() and tried it in an if statement rather than a while. No luck. It must be permissions since it's finding the attachments but the ACLs and the cross scope potential issue has been remediated. Any assistance?
1 ACCEPTED SOLUTION

harshav
Tera Guru

You can update your script to like this

 

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', '');  //The sys_id is between the quotes but redacted
gr.query();
while(gr.next()){
    gs.info('Found attachment');  //This one does print
    gs.info('Can Delete Attachment :' + gr.canDelete()); //This one gives me true
    gs.info('File Name to be Deleted ' + gr.file_name); //This one gives me the proper file name
  if(gr.canDelete()){
    var attachmentSysID = gr.sys_id;    
    var attachment = new GlideSysAttachment(); 
    attachment.deleteAttachment(attachmentSysID);
  }
}

 

View solution in original post

2 REPLIES 2

harshav
Tera Guru

You can update your script to like this

 

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', '');  //The sys_id is between the quotes but redacted
gr.query();
while(gr.next()){
    gs.info('Found attachment');  //This one does print
    gs.info('Can Delete Attachment :' + gr.canDelete()); //This one gives me true
    gs.info('File Name to be Deleted ' + gr.file_name); //This one gives me the proper file name
  if(gr.canDelete()){
    var attachmentSysID = gr.sys_id;    
    var attachment = new GlideSysAttachment(); 
    attachment.deleteAttachment(attachmentSysID);
  }
}

 

Thank you! That worked perfectly. After hours of struggling, it now works. Thank you so much!