Need to download selected files from the attachment

G Phani Krishna
Kilo Contributor

I am using OOB functionality Attachments related list and i cannot able to download selected files,when i download those specific files it's downloading all the files other than the selected files.PFB

 

 

Could you please provide the script to download specific files.

 

15 REPLIES 15

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Krishna,

I remember similar question from you earlier to which I posted solution

Can you share the code for the Download UI action?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Earlier requirement was different.Now i want the option in attachment related list to download selected files.Could you please help me with code ankur.

 

Thanks,

Pratheep A

Hello ankur,

In earlier requirement we can able to download selected files in form UI action,But when it comes to attachment related list post selection and it's agin poping the box for to select the files.PFB

 

find_real_file.png

 

 

Hi Krishna,

Similar things you can do here; there in UI action you were calling UI page and sending the record sys_id to get the attachment records

Now here since you already have the attachment records added as related list; the list choice UI action can pick the record sys_ids directly

Update the UI action as below; you will send the checked records sys_ids now

function openPage(){

        var sysIdArray = g_list.getChecked();
	var gDialog = new GlideDialogWindow("show_attachments");
	gDialog.setTitle("Display & Download Attachments");
	gDialog.setSize(800,800); //Set the dialog size
	gDialog.setPreference('sysparm_checkedValues', sysIdArray.toString());
	gDialog.render();
}

UI page updated code: now you will query sys_attachment table with the sys_id of the records and then iterate over it

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<style>
		table {
		font-family: arial, sans-serif;
		border-collapse: collapse;
		}

		td, th {
		border: 1px solid #dddddd;
		text-align: left;
		padding: 8px;
		}

		tr:nth-child(even) {
		background-color: #dddddd;
		}
	</style>

	<g:evaluate jelly="true" object="true" var="jvar_jsonObj">
		var recordArr = RP.getWindowProperties().get('sysparm_checkedValues');
		var arr = [];
		var gr = new GlideRecord('sys_attachment');
		gr.addQuery('sys_id', 'IN' , recordArr);
		gr.query();
		while(gr.next()){
		var jsonObj = {};
		jsonObj.name = gr.getValue('file_name');
		jsonObj.path = '/sys_attachment.do?sys_id=' + gr.getValue('sys_id');
		arr.push(jsonObj);
		}
		arr;
	</g:evaluate>	

	<j:choose>
		<j:when test="${arr.length > 0}">
			<table cellspacing="0" cellpadding="0" width="100%">
				<th>File Name</th>
				<th>Path</th>
				<j:forEach items="${jvar_jsonObj}" var="jvar_json">                             
					<tr><td>${jvar_json.name}</td><td><a href="${jvar_json.path}">Click here to download this file</a></td></tr>
				</j:forEach> 
			</table>	 
		</j:when>
			<j:otherwise>
			<p>No attachments to display</p>
			</j:otherwise>
	</j:choose>

</j:jelly>

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