Not able to delete attachment from Data Source

amala2
Mega Guru

I am using the below code in Post Script of Scheduled Data Import so as to delete attachment from Data Source.But it is not deleting the attachment.If I try this code from "background script" ,it is working fine.

var dataSourceID = 'xxxxxxxx'; // data source sys id
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sys_data_source');
gr.addQuery('table_sys_id', dataSourceID);
gr.query();
if(gr.hasNext())
gr.deleteRecord();

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Amala,

I remember similar question has been posted on community earlier and has been answered

https://community.servicenow.com/community?id=community_question&sys_id=5c34228adbe2f3440be6a345ca96...

try to update code as below and check;

also in which scope you are running this scheduled import i.e. global or custom scope

if you are in custom scope app then it blocks deleting attachment from the other scope; i.e. so you need to enable can delete checkbox for sys_attachment table under application access when you open the table

var dataSourceID = 'xxxxxxxx'; // data source sys id
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sys_data_source');
gr.addQuery('table_sys_id', dataSourceID);
gr.query();
if(gr.next()){

gs.info('Can Delete Attachment :' + gr.canDelete());
gr.deleteRecord();

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Amala,

Any update on this?
Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur

 

I tried this, but I got an error as below. I did enable 'Can delete' on sys_attachment table. Can you help here

"Error Message

Delete operation against 'sys_attachment' from scope 'sn_vul' has been refused due to the table's cross-scope access policy

Hi Tena,

I would encourage to create new thread for this question as this question is already answered.

It is related to cross scope privilege access

Post the link here and it can be checked

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader