UI action to export specific fields to PDF

Carlos52
Tera Expert

Hi everyone,

 

I need to create an UI action to export certain fields from a ticket to a pdf file. I followed the instructions on previous topics - UI Action Button to Access Export > PDF Portrait and  UI Action to export a form to pdf , however the resulting pdf includes all the fields:

function export2PDF() { 
 var sysparm_table = g_form.getTableName(); 
 var sysparm_sys_id = g_form.getUniqueValue().toString(); 
 var instanceName = 'https://my-instance.service-now.com/';
 var url = instanceName+sysparm_table + '.do?PDF&sys_id=' + sysparm_sys_id; 
 g_navigation.openPopup(url);
}

 

Does anyone know how to modify the previous code to only include certain fields?

Or alternatively how to create a pdf with only certain values.

 

Thanks in advance for any help,

Carlos

 

 

1 ACCEPTED SOLUTION

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

as you are using a URL to get it exported to PDF the resulting PDF will contain the fields of the form view specified. In your case (if no view is specified) that view is the default view.

If you want different fields to be exported this way, the easiest approach is to create a new form view with only these fields. All you have to do is add the view to the URL

var url = instanceName+sysparm_table + '.do?PDF&sys_id=' + sysparm_sys_id + "&sysparm_view=PDF";

This should result in the PDF being created with the new view.

Note: you can make this view hidden, so a user cannot accidentally select it. This KB article describes it best.

Hope this helps,

Regards,
Fabian

View solution in original post

7 REPLIES 7

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

as you are using a URL to get it exported to PDF the resulting PDF will contain the fields of the form view specified. In your case (if no view is specified) that view is the default view.

If you want different fields to be exported this way, the easiest approach is to create a new form view with only these fields. All you have to do is add the view to the URL

var url = instanceName+sysparm_table + '.do?PDF&sys_id=' + sysparm_sys_id + "&sysparm_view=PDF";

This should result in the PDF being created with the new view.

Note: you can make this view hidden, so a user cannot accidentally select it. This KB article describes it best.

Hope this helps,

Regards,
Fabian

Hey Fabian,

 

Thanks for your help, that worked!

It is a shame that the pdf is still created with some extra info that is not needed (who requested the pdf, at what time, etc) but at least only the fields on that view are printed. 🙂

 

Cheers,

Carlos

Hey @Carlos52 How can we make it work if we have dependent fields? For eg: the PDF displays all the fields in that current view even if that field is not on the form because it is a dependent field. How do we cater that scenario to only show dependent field when they are being set?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can check point mentioned by Fabian and share us the update once you try that out.

Regards
Ankur

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