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 ,

 

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

 

 

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)

ayan_murshad
Tera Guru
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.query();
    while (attach.next()) {
        attach.deleteRecord();
    }

}

 Try this

SANDEEP28
Mega Sage

@Rakesh40 Use below code. When you are writing code in background script or in any scripting module, make sure you are using proper naming convention for variables or object as part of best scripting practices. It will help you.

 

var grRequestItem = new GlideRecord('sc_req_item');
grRequestItem.addQuery('cat_item', '62f4956bdbc4285455e326c2ca96191a');// Item Sys ID
grRequestItem.query();
while (grRequestItem.next()) {
    var grAttach = new GlideRecord('sys_attachment');
    grAttach.addQuery('table_sys_id', grRequestItem.sys_id);
    grAttach.addQuery('table_name','sc_req_item');
    grAttach.query();
    while (grAttach.next()) {
        grAttach.deleteRecord();
    }

}

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!