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

Harish KM
Kilo Patron
Kilo Patron

Check the below thread

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

https://community.servicenow.com/community?id=community_question&sys_id=48cfcb65dbdcdbc01dcaf3231f961969

Regards
Harish

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

As you mentioned, I was trying to delete it from the Custom Scope.

Thanks for your help

Vishal Khandve
Kilo Sage

Hi Amala,

can you please try below code-

I've tried it using background script.


var gr1= new GlideRecord('sys_data_source');
gr1.addQuery('sys_id',' ');
gr1.query();
if(gr1.next()){
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sys_data_source');
gr.addQuery('table_sys_id', gr1.sys_id);
gr.query();
if(gr.next()){
gs.log("Test"+gr.file_name);
gr.deleteRecord();
}
}

 

Thanks,

Vishal

 

If this reply assisted you, please consider marking Correct or Helpful it.