How to auto-download record PDF file to user's local PC after record insert/update?

Aki18
Tera Contributor

When an user insert or update the record, I would like the browser to automatically download the record as PDF to the user's local PC.

Of course I understand that the user can download the record manually from the form context menu, but I need to automate it.

I think some logics are needed in Business Rule, but could someone please advise me on what kind of script can achieve this?

 

Aki18_0-1688452214351.png

 

Best Regards,

Aki

1 ACCEPTED SOLUTION

All methods presented in this thread seem to be over complicated...

You need two new scripts:

1.) A new Business Rule:
Name: Set PDF Export
Table: Incident/Requested Item/wherever the export should happen
When: before
Advanced: true
Filter Conditions: <this should reflect whenever a pdf should be exported>
Insert: True
Update: True
Condition: gs.isInteractive()

Script:

 

(function executeRule(current, previous /*null when async*/) {

	var view = gs.action.getGlideURI().get('sysparm_view');
	gs.getUser().savePreference('onetime_export_pdf', JSON.stringify({
		table: current.getRecordClassName(),
		sys_id: current.getUniqueValue(),
		view: view,
		domain: current.getValue('sys_domain')
	})); 

})(current, previous);

 

2.) A new UI Script (sys_ui_script.do):
API Name: AutoExport PDF
UI Type: Desktop
Global: True
Script:

 

addAfterPageLoadedEvent(function () {
	var exportPDF = getPreference('onetime_export_pdf');
	if (exportPDF) {
		try {
			var pdfData = JSON.parse(exportPDF);
			if (pdfData.table && pdfData.sys_id) {
				var isLandscape = true;
				exportToPDF(pdfData.table, pdfData.sys_id, isLandscape, pdfData.view, pdfData.domain);
			}
		} catch (e) {

		} finally {
			setPreference('onetime_export_pdf', '');
		}
	}
});

 


The idea is that the business rule sets a "one time" user preference that a certain record shall be exported to PDF.

Whenever the user loads the page the next time, this user preference is checked and the PDF is exported. After the export the User Preference is cleared ("one time" export only).

 

If this solves your issue, a "Helpful + Mark Answer as Solution" is highly appreciated 🙂

View solution in original post

17 REPLIES 17

AirSquire
Tera Guru

You can Navigate to the PDF context menu

Screenshot 2023-07-04 at 12.59.36 PM.pngOpen the Record and in the Related list open your desired format. I am taking the Portrait as example

Screenshot 2023-07-04 at 1.01.59 PM.png

From there you can utilize the code and paste it in your Save / Submit / Update UI Action to get the same functionality

Screenshot 2023-07-04 at 1.04.43 PM.png

Aki18
Tera Contributor

Hi @AirSquire ,

Thank you for the reply. I copied the script you provided and created the following after insert/update Business Rule.

However, the PDF file is not downloaded after updating the record. Can you please check it?

 

(function executeRule(current, previous /*null when async*/) {
   
   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();
  
})(current, previous);

 

The APIs used in the code are client side. You have to keep them in the UI Action not in the BR. Since BR run server APIs

Aki18
Tera Contributor

Hi @AirSquire ,

I would not like to modify the OOTB UI Actions, so I need to create new BR for the requirement.

I tried to convert the script to fit the server side script for BR, but not working...

Could you please help me with troubleshooting it?

(function executeRule(current, previous /*null when async*/) {
   
   var sysparm_rows = current.getAggregate().getRowCount();
   var num_rows = parseInt(sysparm_rows);
   var sysparm_query = current.getEncodedQuery();

   var sysparm_view = current.view;
   if (num_rows < g_export_warn_threshold) {
      var dialog = new global.GwtPollDialog(current.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_pdf');
      dialog.execute();
      return;
   }
   var dialog = new global.GwtExportScheduleDialog(current.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_pdf');
   dialog.execute();
  
})(current, previous);