Delete Attachment

Jenjoe12
Tera Contributor

Hi All

I have record producer "Reporting "associated with case table. The requirement is to delete the attachment for the case records for that related "Reporting" record producer and also know how much attachment records have been deleted. Can we achieve this by executing the query via background script? Seeking your help!

 

1 REPLY 1

AnirudhKumar
Mega Sage
Mega Sage

Yes. There's a table called Item Produced Records (sc_item_produced_record).

Every time you submit a record producer, an record is created in this table with a reference to the target record.

So, that's where you begin.

 

Have your script query this table, get the case record, then query the attachment table using the case ID.

Finally delete the attachments.

 

var rpObj = new GlideRecord("sc_item_produced_record");
rpObj.setLimit(1);
rpObj.addQuery("producer.name", "Report Performance Problem"); //Add your record producer name here
rpObj.query();
while (rpObj.next()) {

    var fileObj = new GlideRecord("sys_attachment");
    fileObj.addQuery("table_sys_id", rpObj.task.toString());
    fileObj.query();

    while (fileObj.next()) {
		gs.info('Deleting ' + fileObj.file_name +  ' from ' + rpObj.task.number);
        fileObj.deleteRecord();
    }

}