Export Record to PDF
Add a form button to export the current record to a PDF document.
Form Button Table: incident
#ui-action #form-button #pdf #export #print #client
Script Code
JavaScript
1// Client-side code
2function exportToPDF() {
3 // Get current record sys_id
4 var sysId = g_form.getUniqueValue();
5 var tableName = g_form.getTableName();
6 var recordNumber = g_form.getValue('number');
7
8 // Confirm action
9 var confirmed = confirm('Export ' + recordNumber + ' to PDF?');
10 if (!confirmed) {
11 return;
12 }
13
14 // Show loading message
15 g_form.addInfoMessage('Generating PDF...');
16
17 // Method 1: Use ServiceNow's built-in PDF generation
18 // This opens the record in print view which can be saved as PDF
19 var printUrl = '/' + tableName + '_print.do?sys_id=' + sysId;
20 window.open(printUrl, '_blank');
21
22 // Method 2: Use custom PDF generation via Script Include (uncomment to use)
23 /*
24 var ga = new GlideAjax('PDFGenerator');
25 ga.addParam('sysparm_name', 'generatePDF');
26 ga.addParam('sysparm_table', tableName);
27 ga.addParam('sysparm_sys_id', sysId);
28
29 ga.getXMLAnswer(function(response) {
30 if (response) {
31 var result = JSON.parse(response);
32
33 if (result.success) {
34 // Download the generated PDF
35 window.location.href = result.download_url;
36 g_form.addInfoMessage('PDF generated successfully');
37 } else {
38 alert('Error generating PDF: ' + result.message);
39 }
40 }
41 });
42 */
43}
How to Use
1. Create a new UI Action on your table
2. Set Name to "Export to PDF"
3. Set Table to "incident" (or your table)
4. Check "Form button" checkbox
5. Check "Client" checkbox
6. Set Order: 200
7. Add condition: !current.isNewRecord()
8. Paste the code above
9. For custom PDF generation, create PDFGenerator Script Include
10. Test by opening a record and clicking Export to PDF