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

LaurentChicoine
Tera Guru

With a SaaS/PaaS you should not worry to much about storage, but



your gr.addQuery("table_sys_id", scTask);



should probably be:



gr.addQuery("table_sys_id", "IN", scTaskID);



Edit: Also, why do you update scTask?


paramveer
Mega Guru

Change the code like below   :



gr.addQuery("table_sys_id",scTaskID); // scTask is wrong



~Paramveer Singh


~Senior Developer



Please mark Helpful, Like, or Correct Answer if applicable.


Chuck Tomasi
Tera Patron

Hi Raphael,



I agree, don't worry about storage. That's part of what you pay us for. 🙂



FYI - I noticed a couple other things about your script.



First, your scTask.update() inside the while, doesn't actually do anything since you didn't update the record. Are you trying to update it unconditionally or something else missing here (like setting/change a value) that I missed? Second, If you want to bulid that that scTaskID string a little more efficiently, use an array (figuring out where commas go - let the computer do it.)



Something like



var scTaskList = [];



// query is unchanged


while (scTask.next()) {


        scTaskList.push(scTask.getValue('sys_id'));


}


scTaskID = scTaskList.join(',');


// Rest of the script continues



BTW, thanks for the screen shots and good use of getValue()!


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