← Script library

UI Actions

Export Record to PDF

Add a form button to export the current record to a PDF document.

JavaScript
// Client-side code
function exportToPDF() {
  // Get current record sys_id
  var sysId = g_form.getUniqueValue();
  var tableName = g_form.getTableName();
  var recordNumber = g_form.getValue('number');

  // Confirm action
  var confirmed = confirm('Export ' + recordNumber + ' to PDF?');
  if (!confirmed) {
    return;
  }

  // Show loading message
  g_form.addInfoMessage('Generating PDF...');

  // Method 1: Use ServiceNow's built-in PDF generation
  // This opens the record in print view which can be saved as PDF
  var printUrl = '/' + tableName + '_print.do?sys_id=' + sysId;
  window.open(printUrl, '_blank');

  // Method 2: Use custom PDF generation via Script Include (uncomment to use)
  /*
  var ga = new GlideAjax('PDFGenerator');
  ga.addParam('sysparm_name', 'generatePDF');
  ga.addParam('sysparm_table', tableName);
  ga.addParam('sysparm_sys_id', sysId);

  ga.getXMLAnswer(function(response) {
    if (response) {
      var result = JSON.parse(response);

      if (result.success) {
        // Download the generated PDF
        window.location.href = result.download_url;
        g_form.addInfoMessage('PDF generated successfully');
      } else {
        alert('Error generating PDF: ' + result.message);
      }
    }
  });
  */
}

How to use it

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

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.