- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 05:19 AM
Hi community,
I'm looking for delete attachment when my task is closed (to save storage)
Actually i'have a workflow :
And i written this script but it doesn't work
Have you got a solution to help me ?
Thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 05:35 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 05:23 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 05:30 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 05:31 AM
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()!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 05:35 AM
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