Remove attachments on Request items

Rakesh40
Tera Contributor

Hello Everyone,

I want to delete the attachments that are linked to specific item from Request Item.

I'm trying to write a fix script  but it is not working.

 

var inuser = new GlideRecord('sc_req_item');
inuser.addQuery('cat_item', '62f4956bdbc4285455e326c2ca96191a');// Item Sys ID
inuser.query();
while (inuser.next()) {
    var attach = new GlideRecord('sys_attachment');
    attach.addQuery('file_name', inuser.sys_id);
    attach.query();
    while (attach.next()) {
        attach.deleteRecord();
    }

}

 

 

Thanks

2 ACCEPTED SOLUTIONS

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Rakesh40 ,

 

Plz try the below code. with rollback option enabled as we are trying to delete record. I have added two logs plz run the script as it is n check the logs if u r satisfied then remove the comment from delete record command & execute it.

 

 

var inuser = new GlideRecord('sc_req_item');
inuser.addQuery('cat_item', '62f4956bdbc4285455e326c2ca96191a');// Item Sys ID
inuser.query();
while (inuser.next()) {
    var attach = new GlideRecord('sys_attachment');
    attach.addQuery('table_sys_id', inuser.sys_id);
    attach.addQuery('table_name','sc_req_item');
    attach.query();
    while (attach.next()) {
gs.info('Sys ID of attachment'+ attach.sys_id);
gs.info('FileName of attachment'+ attach.file_name);
        //attach.deleteRecord();
    }

}

 

 

Thanks,

Danish

 

 

View solution in original post

Samaksh Wani
Giga Sage
Giga Sage

Hello @Rakesh40 

 

Modify your script with mine :-

 

 attach.addQuery("table_sys_id", inuser.sys_id);

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh Wani (Rising Star 2023)

View solution in original post

6 REPLIES 6

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Rakesh40 ,

 

If our responses helped you could you please mark it as Solution Accepted so that other users could benefit from it.

 

Thanks,

Danish

 

Samaksh Wani
Giga Sage
Giga Sage

Hello @Rakesh40 

 

If my response helped in solving your query, Please mark it as Accepted. It will other community users.

 

Regards,

Samaksh