- 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-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-20-2023 08:02 AM
Hi @Markus Kraus ,
Thank you again for your help!
I tried with your scripts and the auto-download worked when updating/inserting the record, which is great.
By the way, is there any docs that defines "exportToPDF" method? I ran a search but couldn't find it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2023 08:47 AM
Hi @Aki18 You're welcome. The exportToPDF unfortunately isn't explicitly documented, it is simply the method which is called when you click "Export to PDF" on the header context menu of a header.
It's not very likely that this method will ever change. There is also a way to export as PDF by simply adding PDF to the url. For example: .../incident.do?PDF&sys_id=<the sysid>
But I'd leave it like it is with the exportToPDF 😉
If you don't have any open questions, consider marking my previous answer as the "solution" so other community members running into the same question know there is a solution 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2024 01:40 AM - edited ‎03-06-2024 01:43 AM
Hi @Markus Kraus, Your script worked for me and It was Helpful.
But can we download the same PDF on a record instead of Downloading to the Local PC.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 01:50 AM
Then we're talking about a "Generate PDF"-Business Rule right?