UI action to show list of attachments attached to a form

Nikhil55
Tera Contributor

Can anyone provide UI action script to show list of attachments attached to the form. The functionality will be same as OOB paper clip attachment button present in every form but this ui action should not provide options to users to remove/add attachments. it will just show list of attachments and when clicked on attachment it should open so that user can read it.

1 ACCEPTED SOLUTION

Hi @Nikhil55 ,

 

For this you will have to modify your display BR and UI page both. Make the below mentioned changes :

 

1. Modify your display Business rule as shown below:

 

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var files = [];
	
	var attachGr = new GlideRecord("sys_attachment");
	attachGr.addQuery("table_sys_id",current.getUniqueValue());
	attachGr.query();
	while(attachGr.next()){
		var info = {};
	info.file_name = attachGr.getValue("file_name");
	info.link = attachGr.getLink();
	files.push(info);
	}
	
	g_scratchpad.filesUploaded = JSON.stringify(files);
	

})(current, previous);

 

kamleshkjmar_0-1666776779296.png

 

2. Modify the UI page as shown below:

 

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<div id="attachments_uploaded">
	
	</div>
	<script>
		var files = g_scratchpad.filesUploaded !="" ? g_scratchpad.filesUploaded : "";
		files = JSON.parse(files);
		var message = "";
		ele = document.getElementById('attachments_uploaded');
	//alert(files)
	if(files != ""){
		//var fileNames = files.split(",");
		files.forEach(function(fName){
			//console.log(fName)
		message += "<a href ="+fName.link + " >" +fName.file_name + "</a>"+"<br/>";
		
		
		})
	}
		//alert(message)
		ele.innerHTML = message
	</script>
</j:jelly>

 

kamleshkjmar_1-1666776835039.png

 

 

kamleshkjmar_0-1666777116637.png

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh

 

View solution in original post

10 REPLIES 10

kamlesh kjmar
Mega Sage
Mega Sage

Hi @Nikhil55 ,

 

Below is the way to achieve this:

 

1. Create a Display BR that will fetch all the attachments attached to the record. I am using incident table for this example.

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var files = "";
	var attachGr = new GlideRecord("sys_attachment");
	attachGr.addQuery("table_sys_id",current.getUniqueValue());
	attachGr.query();
	while(attachGr.next())
		files += attachGr.getValue("file_name") + ",";
	
	
	g_scratchpad.filesUploaded = files;
	

})(current, previous);

 

 

kamlesh8_0-1666696739403.png

 

2. Create a UI page, where you will be fetching data sent by display BR and will use to create a dynamic content having all the attached file's name.

 

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<div id="attachments_uploaded">
	
	</div>
	<script>
		var files = g_scratchpad.filesUploaded !="" ? g_scratchpad.filesUploaded : "";
		var message = "";
		ele = document.getElementById('attachments_uploaded');
	if(files != ""){
		var fileNames = files.split(",");
		fileNames.forEach(function(fName){
		message += fName + "<br/>";
				
		})
	}
		ele.innerHTML = message
	</script>
</j:jelly>

 

 

kamlesh8_1-1666696903376.png

You can modify the HTML/CSS of the page as per your need, I am helping here how to get the data and show on form.

 

3. Finally write a client callable UI action and call this UI page on click of it

 

function showAttachment() {
    var x = new GlideModal("attachments");
    x.setTitle("Attachments List");
    x.render();
}

 

 

kamlesh8_2-1666697121999.png

 

Now open any incident record and click on the Show Attachments button, it will popup list of all attachments, attached with the opened record.

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh

 

 

 

 

Hi @kamlesh kjmar ,

I have followed the steps you mentioned but I'm getting below error:

Nikhil55_1-1666710587449.png

 

 

Hi @Nikhil55 ,

 

You have to create UI page with the name attachments  first as shown in the screen shot below:

kamlesh8_0-1666712051014.png

 

Below is the ui page script used:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<div id="attachments_uploaded">
	
	</div>
	<script>
		var files = g_scratchpad.filesUploaded !="" ? g_scratchpad.filesUploaded : "";
		var message = "";
		ele = document.getElementById('attachments_uploaded');
	if(files != ""){
		var fileNames = files.split(",");
		fileNames.forEach(function(fName){
		message += fName + "<br/>";
				
		})
	}
		ele.innerHTML = message
	</script>
</j:jelly>

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh

Hi @kamlesh kjmar , Thank you! I can see the list of attachments. but I want the file names as a link so that when I click on the file I can able to open that document and view. Is there any way to achieve that?

Nikhil55_0-1666763384892.png