Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Assistance Needed: Export Emails as PDF from Incident Form

mania
Tera Contributor

 

Hi,

I would like to create an “Export Email” button on the Incident form (under the Email tab, as shown in the attached screenshot).

When the button is clicked, it should download a single PDF file that includes:

  • All email communications

  • Subject

  • Email body

  • Attachments

Could you please suggest the best approach to implement this? Any guidance would be greatly appreciated.

Thanks in advance.

6 REPLIES 6

Ankur Bawiskar
Tera Patron

@mania 

you can create a Related List Action on that Emails table and it should be seen only when parent table is incident

Make it server side and use PDF Generation API to create pdf file and attach to incident record

you can use PDF Generation API

check this link which has script from Piyush

Generate PDF file for catalog variables and attach it to RITM 

Generate PDF and attach to the record

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@mania 

Thank you for marking my response as helpful.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

yashkamde
Mega Sage

Hello @mania ,

 

I didn't get specifically where you wanted the ui action !
However I configured it, according to your requirement it is downloading the pdf including subject, body & attachments..
Screenshot 2026-03-02 143808.png

 

UI Action -> 

Screenshot 2026-03-02 142552.png

 

Script :

var v = new sn_pdfgeneratorutils.PDFGenerationAPI();
var html = "<h2>Email Communications</h2>";

var emailGR = new GlideRecord('sys_email');
// emailGR.addQuery('target_table', 'incident');
emailGR.addQuery('instance', current.sys_id);
emailGR.query();

while (emailGR.next()) {
    html += "<div style='margin-bottom:20px;'>";
    html += "<b>Subject:</b> " + emailGR.subject + "<br/>";
    html += "<b>Body:</b><br/>" + emailGR.body + "<br/>";

    // Fetch attachments for this email
    var attachGR = new GlideRecord('sys_attachment');
    attachGR.addQuery('table_sys_id', emailGR.sys_id);
    attachGR.query();
    while (attachGR.next()) {
        html += "<b>Attachment:</b> " + attachGR.file_name + "<br/>";
    }
    html += "</div>";
}

var result = v.convertToPDF(html, 'incident', current.sys_id, 'Incident Email PDF');
if (result.status === 'success' && result.attachment_id) {
    var downloadURL = '/sys_attachment.do?sys_id=' + result.attachment_id;
    var gURL = new GlideURL(downloadURL);
    action.setRedirectURL(gURL);
	gs.addInfoMessage('PDF downloaded');
} else {
    gs.addErrorMessage('PDF generation failed or no attachment ID returned.');
}

 

If my response helped mark as helpful and accept the solution.

mania
Tera Contributor

@yashkamde ,

Thankyou for helped on this, i enhanced the code as per my functionality so it worked.

Thankyou!