How to remove attachments from a record in scoped application using Business rule

savitha5
Tera Contributor

Hi All,

I have got a requirement to delete attachments from a record in HR Core Case table when the field HR service is changed from General Inquiry to any value. I tried to achieve this using After Business rule on HR Core Case table, but since the Business rule is on Scoped application and Sys_attachment is on Global it is giving an error message as 'Delete operation against 'sys_attachment' from scope 'sn_hr_core' has been refused due to the table's cross-scope access policy'

Below is my Business rule.

when: After
Table: sn_hr_core_case
Script:

var att= new GlideRecord('sys_attachment');
    att.addQuery('table_name','sn_hr_core_case');
    att.addQuery('table_sys_id',current.sys_id);
    att.query();
    while(att.next()){
        att.deleteMultiple();
    }

The Business rule works fine if I switch to Global and edit Sys_attachment table setting and check the can delete option under Access Control tab as shown below screenshot. But I dont want to make any changes directly on Sys_attachment table, because it may affect something else later.

find_real_file.png

 

Can anyone help me to fix this.

 

Thanks in Advance,
Savitha

 

6 REPLIES 6

Ben Rowny
Tera Guru

Hi,

I know this is an old question, but it came up in my search for an answer to the same...

I found that you do NOT have to modify the OOTB sys_attachment Application Access or add a Cross Scope policy.  Instead you can just use the scoped version of GlideSysAttachment:

function deleteAllAttachments(record){
	// Utility function removes all the previous attachments
	// Must use GlideSysAttachment in scope
	var gsa = new GlideSysAttachment();

	var att = new GlideRecord('sys_attachment');
	att.addQuery('table_name',record.getTableName());
	att.addQuery("table_sys_id",record.sys_id);
	att.query();
	while(att.next()){
		gsa.deleteAttachment(att.sys_id);
	}
}

I believe this should also respect whether your current scope can access the table your attachments are attached to, which is important, and keeps security tight on your sys_attachment table.

ServNowDev
Tera Guru

 

 var dataSource = 'mydatasource sysid';
 


 var attachmentGR = new GlideRecord('sys_attachment');
 attachmentGR.addQuery('table_sys_id', dataSource);
 attachmentGR.query();

while (attachmentGR.next()) {

    var gsa = new GlideSysAttachment();
     gsa.deleteAttachment(attachmentGR.sys_id.toString());

     gs.info('Attachment deleted: ' + attachmentGR.sys_id);
     

 }

 

 

having the same issue but found your source here, it worked in global but not in my scope application