Can I prepare a view for PDF export and automatically switch views when exporting?

bonsai
Mega Sage

I have a view for PDF export that is separate from the view the user operates.

One way to do this is to switch views each time the export is made, but if possible I'd like to automatically specify the view for PDF export when exporting.

Is this possible?

2 REPLIES 2

Itallo Brandão
Giga Guru

Hi bonsai,

Yes, this is absolutely possible and is a great pattern to keep the UI clean while ensuring official documents use a strict layout.

You can achieve this by creating a Client-side UI Action. Instead of using the context menu, you construct a specific URL that passes the sysparm_view parameter directly to the PDF processor. This generates the file in the desired view without changing the user's current session view.

Here is the implementation:

  1. Create a UI Action

    • Name: Export PDF (Custom View)

    • Client: Checked (true)

    • Form Context Menu: Checked (or Form Button)

    • Onclick: exportSpecialPDF()

  2. The Script:

function exportSpecialPDF() {
    var sysId = g_form.getUniqueValue();
    var tableName = g_form.getTableName();
    var targetView = 'your_pdf_view_name'; // Replace with your actual view name
    
    // Construct the URL with the view parameter and the PDF flag
    var url = tableName + '.do?sys_id=' + sysId + '&sysparm_view=' + targetView + '&PDF';
    
    // Open the download in a new window/tab so the current form doesn't reload
    g_navigation.open(url, '_blank');
}


Why this works:
The &PDF processor respects the sysparm_view passed in the URL string independently of the view currently rendered in the user's browser frame.


If this response helps you solve the issue, please mark it as Accepted Solution.
This helps the community grow and assists others in finding valid answers faster.

Best regards, Brandão.

Ankur Bawiskar
Tera Patron

@bonsai 

how is user exporting? from some custom UI action on form?

share some details or screenshots

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