****I want to download selected files from incident instead of all could you please help with the script***

G Phani Krishna
Kilo Contributor

****I want to download selected files from incident instead of all could you please help with the script***

1 ACCEPTED SOLUTION

Hi Krishna,

So here is the updated UI page code which shows checkbox besides every file; you need to click the checkbox for which file to be downloaded

then click on the download attachments and it will download

UI Page Code:

<?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 recordSysId = RP.getWindowProperties().get('sysparm_sysID');
		var arr = [];
		var gr = new GlideRecord('sys_attachment');
		gr.addQuery('table_sys_id', recordSysId);
		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>Download Yes/No</th>
				<j:forEach items="${jvar_jsonObj}" var="jvar_json">                             
					<tr><td>${jvar_json.name}</td><td><input class="checkbox" type="checkbox" name="type" id="${jvar_json.path}"></input></td></tr>
<!-- 					<a href="${jvar_json.path}">Click here to download this file</a> -->
				</j:forEach> 
			</table>
			<button type="submit" value="download_attachments" onclick="downloadFiles()">Download Specific Attachments</button>
		</j:when>
			<j:otherwise>
			<p>No attachments to display</p>
			</j:otherwise>
	</j:choose>
</j:jelly>

Client Script Code:

function downloadFiles(){

	var selected = [];
	$j("input:checkbox[name=type]:checked").each(function() { 
                selected.push($j(this).attr('id')); 
            }); 
	for(var i=0;i<selected.length;i++){
		
		var url = selected[i];
		window.open(url);
		
	}
}

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

View solution in original post

62 REPLIES 62

Hi Deepika,

I won't encourage using the out of the box attachment UI page since that is global and would be used throughout the instance

So hence create new one which caters to your requirement and customize that

Regards

Ankur

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

i think the same issue is happening with me, when selected multiple files, only one (first one) is being downloaded everytime when button is clicked.

I tried reading the above conversations, but lightly stressed out as it is like this:

h

i

h

o

w

a

r

e

So could not figure out the solution for this. 

I figured out a solution for this. 

function downloadFiles(){

var selected = [];
$j("input:checkbox[name=type]:checked").each(function() {
selected.push($j(this).attr('id'));
});
//alert(selected);
var url=[];
var num = selected.length;
//var i=0;
while(num>0){
url[num-1]= selected[num-1];
alert(url[num-1]);
window.location=url[num-1];
--num;
}
}

The alert in the while loop prompts the user to click on it everytime the file has to be downloaded. It works well.

But i there a possibility that i can download all of them at once in a zip file?

Hi Ankur, I tried this with the existing UI Page "attachments". Surprisingly the selected attachments are getting removed from the list(ui page). Since i used this is the exixting ui page, i omited few lines of code. 

What i used is:

<td>
<g:evaluate jelly="true" object="true" var="jvar_jsonObj">
var recordSysId = RP.getWindowProperties().get('sysparm_sysID');
var arr = [];
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', recordSysId);
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>
</td>

With the same client script that you used.

Is there anything wrong in the code?