Delete attachment

rapbriqu
Kilo Expert

Hi community,

I'm looking for delete attachment when my task is closed (to save storage)

Actually i'have a workflow :

find_real_file.png

And i written this script but it doesn't work

find_real_file.png

Have you got a solution to help me ?

Thanks

1 ACCEPTED SOLUTION

Bastian Pawlik
Tera Expert

Hi Raphael,



I think the main problem is that the part of the attachment deletion is not within the while-loop of the scTask.


I'm not sure for what you'll need the scTaskID concatenated string, but I refactored your script snippet a bit.



It is untested but this might work:




var scTaskID = '';


var seperator = '';


var scTask = new GlideRecord('sc_task');


scTask.addQuery('request_item', current.sys_id);


scTask.query();


while(scTask.next()){


      var currentTaskID = scTask.getValue('sys_id');


      scTaskID += seperator + currentTaskID;


      seperator = ',';



      //now delete the attachment


      var gr = new GlideRecord('sys_attachment');


      gr.addQuery('table_sys_id', currentTaskID);


      gr.addQuery('table_name', 'sc_task'); //to make 100% sure we delete from the correct table


      gr.deleteMultiple();


}




Best regards,


Basti


View solution in original post

8 REPLIES 8

I try your solution and i think it's working but



the answer is : Delete operation against 'sys_attachment' from scope 'x_43484_font_servi' has been refused due to the table's cross-scope access policy



I'm not fluent in english but i think it's : Hey man you can't do it


Hi Raphael,



Please check Application Access Setting of table "sys_attachment". OOTB "Can Delete" option is unchecked, it means that from other scope you cannot make a delete operation on the attachment table unless and until you explicitly allow this(i.e Set Checkbox "Can Delete" to true).


Screen Shot 2016-06-06 at 6.35.56 AM.png


You can specify what application artifacts are available to other custom applications in different application scopes. These permissions are in addition to the standard access controls (ACLs) that determine whether users can access data in the custom application table from the user interface.


Application Access Settings - ServiceNow Wiki


Hi Raphael,



This has to do with scoped applications and the protection they have between tables. While you COULD go to the sys_attachment table and change the application access to allow other applications delete access (see image below), this would decrease the protection between the various applications and sys_attachment.



Is there a reason you are concerned about the storage used by sys_attachment? Not having to worry about this is one of the features of cloud systems.


find_real_file.png


Hi ctomasi,



For the storage it's not my decision, it's the serviceNow manager (i'm just a developper junior ^^) He said and i do . But he explained me, it's because every instance have XXXXX MB and he is full (and instances are expensives).



thanks for the application scopes now it's working !