Download The History of the record (PDF) and Attachments of record both in a Single Zip File

saichary
Tera Contributor

Hello Community,

 

I have a specific need for a form where clicking a button will generate a single Zip file containing the record's history (in PDF format) and any attachments associated with that record.

 

If the record has no attachments, the download should include only the history. If there are attachments, the download should combine both the record's history and attachments in a Zip file.

 

If anyone has insights or suggestions on how to achieve this, please assist.

 

Regards,
Sai

2 REPLIES 2

cloudops
Tera Expert

To achieve this, you would need to create a custom UI action that will generate the PDF and zip file. Here are the steps:

1. Create a UI Action on the table where you want this functionality.
2. In the UI Action, use the GlideSysAttachment API to get all attachments for the current record.
3. Use the GlideRecord API to get the audit history of the current record.
4. Use the GlideSysAttachment API again to create a PDF from the audit history.
5. Use a third-party library like JSZip (which you would need to upload to your instance as a Script Include) to create a zip file.
6. Add the PDF and all attachments to the zip file.
7. Provide the zip file for download.

Here is a sample code:

javascript
var gr = new GlideRecord('sys_audit');
gr.addQuery('documentkey', current.sys_id);
gr.query();

var auditData = '';
while(gr.next()) {
auditData += gr.getValue('audit_date') + ' ' + gr.getValue('fieldname') + ' ' + gr.getValue('oldvalue') + ' ' + gr.getValue('newvalue') + '\n';
}

var pdf = new PDFCreator();
pdf.addPage(auditData);
var pdfId = pdf.save();

var zip = new JSZip();
zip.file('History.pdf', pdfId);

var ga = new GlideSysAttachment();
var attachments = ga.getAttachments(current);
while(attachments.next()) {
zip.file(attachments.getValue('file_name'), attachments.getValue('data'));
}

var zipId = zip.generate({type:"blob"});

ga.write(current, 'Record.zip', 'application/zip', zipId);


Please note that this is a simplified example and may not work as is. You would need to handle errors and edge cases. Also, creating PDFs and zip files can be resource-intensive, so you should consider the performance implications.

Hi @cloudops 

Thanks for your Reply, I will research this and figure it out soon and get back to you.

Regards,
Sai