- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2023 12:05 AM
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?
Best Regards,
Aki
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2023 12:28 AM - edited ‎07-14-2023 12:29 AM
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 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2023 12:35 AM - edited ‎07-04-2023 12:36 AM
You can Navigate to the PDF context menu
Open the Record and in the Related list open your desired format. I am taking the Portrait as example
From there you can utilize the code and paste it in your Save / Submit / Update UI Action to get the same functionality
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2023 01:15 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2023 01:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2023 02:08 AM
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);