I want to generate a pdf of selected records in a list?

trinadhgottipat
Kilo Expert

Hi All,

I want to generate a PDF of selected records in a list, when the user clicks on 'Generate PDF' UI action(List Choice),created by me.

Is there a way I could do this.

Thanks,

Trinadh.Developer Community Service ManagementdvpChuck Tomasi's BlogPradeep Sharma's Blog ctomasi

1 ACCEPTED SOLUTION

Hi Trinadh,



Have a List UI Action either List bottom button or List context menu as per your need.


Select the checkbox for client and add this function downloadPdf()


In the script section add the following code. What it does is it gets the pdf for only those records which are selected in the list layout.


I have tested it at my end and is working fine. It will take columns specific to a view and not the ones which you have added from gear icon.


If you want the columns which are present on the list layout then make the view as empty



Script


function downloadPdf(){



  var checked = g_list.getChecked(); // get's the sys_id of the checked records


  var query = "sys_idIN" + checked.toString();


  var rows = checked.split(",").length; // rows to be sent to the pdf function


  var view = "default"; // set this to default for columns present in default view for list layout


  //var view = ""; // set this to empty if you want all the columns present in the list layout including the customized ones.



  var dialog = new GwtPollDialog('u_sample_table', query, rows, view, 'unload_pdf');


  dialog.execute();


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

View solution in original post

28 REPLIES 28

larstange
Mega Sage

Hi



You can look at the code from the OOTB Export -> PDF Context menu options and reuse what you need:



/**


* Script executed on the Client for this menu action


*


* The following variables are available to the script:


*       'g_list' the GlideList2 that the script is running against (only valid for List context menus)


*       'g_fieldName' the name of the field that the context menu is running against (only valid for List context menus)


*       'g_sysId' the sys_id of the row or form that the script is running against


*       'rowSysId' is also set to the sys_id of the row to support legacy actions, but g_sysId is preferred


*/


runContextAction();




function runContextAction() {


   


    var sysparm_rows = g_list.grandTotalRows;


    var num_rows = parseInt(sysparm_rows);


    var sysparm_query = g_list.getQuery({all: true});


   


    var sysparm_view = g_list.view;


    if (num_rows < g_export_warn_threshold) {


          var dialog = new GwtPollDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_pdf');


          dialog.execute();


          return;


    }


    var dialog = new GwtExportScheduleDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_pdf');


    dialog.execute();


}


trinadhgottipat
Kilo Expert

Hi Lars,


I have already checked that,but the pdf is generated for all the records irrespective of the selected from the list..



Thanks for the update,


Trinadh


Hi Trinadh,



Have a List UI Action either List bottom button or List context menu as per your need.


Select the checkbox for client and add this function downloadPdf()


In the script section add the following code. What it does is it gets the pdf for only those records which are selected in the list layout.


I have tested it at my end and is working fine. It will take columns specific to a view and not the ones which you have added from gear icon.


If you want the columns which are present on the list layout then make the view as empty



Script


function downloadPdf(){



  var checked = g_list.getChecked(); // get's the sys_id of the checked records


  var query = "sys_idIN" + checked.toString();


  var rows = checked.split(",").length; // rows to be sent to the pdf function


  var view = "default"; // set this to default for columns present in default view for list layout


  //var view = ""; // set this to empty if you want all the columns present in the list layout including the customized ones.



  var dialog = new GwtPollDialog('u_sample_table', query, rows, view, 'unload_pdf');


  dialog.execute();


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,


I have modified my script as per your suggestion.


In my list i have selected 3 records and clicked on my UI action in List Choice.


Then I see this Export Complete along with the message Exported   0 out of 3 rows.


If I click Download,I see this message   'Failed to load PDF' document.



Waiting to hear from you.


find_real_file.png       find_real_file.png




Thanks,


Trinadh