How to identify whether any file attachment present in a table record or not ?

Rathika4
Kilo Explorer

I am new to service now. I Need to list all the records of a table in widget/Service portal along with the attachments if exist. I listed all the records from the table but i want to validate whether each record in a table has an attachment or not. while obtaining the resultset of records how to identify whether the attachment present in a record or not, if present how to get those attachment details. 

Thanks in advance.

1 ACCEPTED SOLUTION

Gaurav Bajaj
Kilo Sage

Hi,

Please try below script in fix script/background script for your table.

This will give you attachment names and record number is attachment exist else it will log a statement that the current record doesn't have an attachment.

var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.query();
while (gr.next()) {
   
	var grAttch = new GlideRecord('sys_attachment');
	grAttch.addQuery('table_sys_id',gr.sys_id); // get attcahments for current incident
    grAttch.query();
	if(grAttch.getRowCount()>0){ //attachment exist
	while(grAttch.next()){
		
		
		gs.print("There is an attachment on"+gr.number+" with name : "+grAttch.file_name);
		
	}
	}
	else{
		
		gs.print("No attachments were found on "+gr.number);
	}
}

Please mark it correct/helpful based on the response.

Thanks
Gaurav

View solution in original post

2 REPLIES 2

Gaurav Bajaj
Kilo Sage

Hi,

Please try below script in fix script/background script for your table.

This will give you attachment names and record number is attachment exist else it will log a statement that the current record doesn't have an attachment.

var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.query();
while (gr.next()) {
   
	var grAttch = new GlideRecord('sys_attachment');
	grAttch.addQuery('table_sys_id',gr.sys_id); // get attcahments for current incident
    grAttch.query();
	if(grAttch.getRowCount()>0){ //attachment exist
	while(grAttch.next()){
		
		
		gs.print("There is an attachment on"+gr.number+" with name : "+grAttch.file_name);
		
	}
	}
	else{
		
		gs.print("No attachments were found on "+gr.number);
	}
}

Please mark it correct/helpful based on the response.

Thanks
Gaurav

Rathika4
Kilo Explorer

Thanks.. worked out