- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 03:47 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 03:54 AM - edited 12-05-2023 03:54 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 03:54 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 03:54 AM - edited 12-05-2023 03:54 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 03:54 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 03:58 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 04:08 AM
@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 !!