Afsar2
Tera Contributor

This article will guide you to export multiple task records from your instance in any file format (PDF).

Purpose: To export multiple task records from view.

OOB feature allows exporting only list views of records.

Approach: Export records to specified file format using URL

URL Format: com-instance.service-now.com/task_table_name.do?PDF&sys_id=taskID

Steps:

  1. Create a UI page with textbox to accept task numbers (comma separated).
  2. Add to client script on the UI page.
  3. Create script include (client callable) to return list of valid sys_id of tasks.

Code Snippets:

1. UI Page

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	
	<g:ui_form>
		
		<input type = "text" id="myListCollector" name = "myListCollector" />
		<br/>

		<g:dialog_buttons_ok_cancel ok = "return exportRecords()"  cancel = "return processCancel()" ok_type="button"  />
		
	</g:ui_form>

</j:jelly>

2. Client script on UI Page

function exportRecords() {
	
		
	var tasks = document.getElementById('myListCollector').value;	

	//fetch sys_id of task records	
	var ga = new GlideAjax('FetchTasks');
	ga.addParam('sysparm_name','fetchTaskIDs');
	ga.addParam('sysparm_task_numbers',tasks); // send task numbers
	ga.getXML(processTaskIds);
	
	//callback function
	function processTaskIds(response) {  
		var answer = response.responseXML.documentElement.getAttribute("answer"); 

		var taskList = JSON.parse(answer);
				
		var i = 0;
		var inte = setInterval(triggerExport, 3000);

		function triggerExport() {

			if (i == taskList.length-1) 
				clearInterval(inte);

			//jslog('taskList number is: '+taskListNum[i]);
			//jslog('taskList number is: '+taskNumbers.options[i].innerHTML);

			//var task_number = taskNumbers.options[i].innerHTML.toString();
			var task_number = taskList[i].taskNumber.toString();

			var task_table = "";
			if(task_number.startsWith("CHG")){
				task_table = "change_request";
			}else if(task_number.startsWith("INC")){
				task_table = "incident";
			}else if(task_number.startsWith("PRB")){
				task_table = "problem";
			}else if(task_number.startsWith("SCTASK")){
				task_table = "sc_task";
			}else if(task_number.startsWith("RITM")){
				task_table = "sc_req_item";
			}else if(task_number.startsWith("REQ")){
				task_table = "sc_request";
			}
			else{
				task_table = "task";
			}
			
			g_navigation.open(task_table+'.do?PDF&sys_id='+taskList[i].taskID);
		
			i++;
		}//triggerExport

	}//processTaskIds
	
	return true;
}

function processCancel(){
	GlideDialogWindow.get().destroy();
}

3. Script Include (Called via AJAX to return list of valid sys_id of tasks.)

Name: FetchTasks

fetchTaskIDs:function() { 
		var tasks = this.getParameter('sysparm_task_numbers');
		
		var taskIdList = [];
		
		var taskNumbers = tasks.split(',');
		
		for(i=0;i<taskNumbers.length;i++){
			
			var taskGR = new GlideRecord('task');
			taskGR.addQuery('number', taskNumbers[i]);
			taskGR.query();
			while(taskGR.next()){
				gs.log("FetchTasks3: id->"+taskGR.sys_id+" num->"+taskGR.number);
				
				var taskObj = {
					"taskNumber": taskGR.getValue('number'),
					"taskID": taskGR.getValue('sys_id')
				};
				
				//taskIdList.push(taskGR.getValue('sys_id'));
				taskIdList.push(taskObj);
			}
			if(taskGR.getRowCount()==0){
				gs.addErrorMessage("Invalid task number:"+taskNumbers[i]);
			}
		}
		
		//return taskIdList.toString();
		return JSON.stringify(taskIdList);
		
	},

 

Please help me improve this with your feedbacks and suggestion.

Please mark helpful and bookmark for future references.

Comments
Marissa N_
Kilo Expert

Hi, I know this is a very old post but it does exactly what we require. We received a list of Case numbers to export in pdf (not able to create a report for this). I've created the UI Page per your instructions (head's up; there's an extra comma at the end of your Script Include which triggers an error so I removed that). The text box appears but if I fill in three Case numbers, separated by comma, then click OK, nothing happens.

 

Is this code still valid? I cannot find any other solution that offers the same functionality (all work with reports/filters).

 

Thanks ahead if you are still out there 🙂

Version history
Last update:
‎01-04-2022 10:39 AM
Updated by: