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

Yes,
Basically my requirement is to automatically generate an OOB PDF on a record when a particular state changes.

Apparently there is no OOTB way to export the record, the only way is to trigger the export via a user that has enough privileges. Unfortunately this means you have to create a user with enough privileges and store its credentials as System Properties...

// ... (inside business rule)
var userName = gs.getProperty('global.pdf_export.user.name');
var userPassword = gs.getProperty('global.pdf_export.user.password');
var recordTable = current.getRecordClassName();
var recordSysID = current.getUniqueValue();
var attachmentFileName = 'pdf_export.pdf';
var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('GET');
rm.setEndpoint(gs.getProperty('glide.servlet.uri') + recordTable + '.do?PDF&sys_id=' + recordSysID);
rm.setBasicAuth(userName, userPassword);
rm.saveResponseBodyAsAttachment(recordTable, recordSysID, attachmentFileName);
var response = rm.execute();

 

There is also a way which would take the current's user session to create the PDF export, but this means that the PDF export would only work in before/after business rules and also only if the user has sufficient privileges to trigger the export.

Hey above one is really helpful but i want to export all the records present in table can you explain how can we do that because when i tried that using business rule with help of query only 1st record is exported.