****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 Krishna,

So have the below approach

1) create a UI action which is client side

2) create an UI page and call this from UI action

3) send the record sys_id to the UI page

4) there create a html tabular structure with 2 columns

Name, File Name, Link

so user can download the attachments individually by clicking on that link

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

Hi Ankur,

 

Thanks for the rply.Could you please help with the script,So that i can try to achive my requirement.

 

Thanks,

Phani

Hi Krishna,

Here are the steps

1) create an UI action on incident table

Name: Show all Attachments & Download

Form button: true

Client: true

Onclick: openPage()

Script below:

function openPage(){
	var gDialog = new GlideDialogWindow("show_attachments");
	gDialog.setTitle("Display & Download Attachments");
	gDialog.setSize(800,800); //Set the dialog size
	gDialog.setPreference('sysparm_sysID', g_form.getUniqueValue());
	gDialog.render();
}

screenshot below

find_real_file.png

 

2) Create an UI page

Name: show_attachments

HTML:

<?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>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>

Testing: once user clicks on the UI Action; user sees the list of attachments and the link; they can then download whichever attachment they want one by one

1) when attachments are nor present

screenshot below

find_real_file.png

 

2) when incidents have attachments:

screenshot below

find_real_file.png

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

Thanks you so much for the time.When we use above am getting below screen and it's not working properly.

 

My requirement i want download only 5 selected attachments out of 10.

 

Could you please help me with script to achievefind_real_file.png this

Hi Krishna,

As part of this approach we are giving users the capability to download attachments 1 by 1

if you want something like below then you need to enhance the code further

1) have checkbox besides every attachment

2) have a button like download attachments

3) iterate over the attachments which are having checkbox as true

4) download only those one by one by opening the 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