- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-11-2019 01:17 PM
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();
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-11-2019 09:32 PM
Hi Amala,
I remember similar question has been posted on community earlier and has been answered
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
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-11-2019 09:18 PM
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
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-11-2019 09:32 PM
Hi Amala,
I remember similar question has been posted on community earlier and has been answered
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
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-17-2019 11:00 AM
As you mentioned, I was trying to delete it from the Custom Scope.
Thanks for your help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-11-2019 10:26 PM
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.