Create a PDF of the triggered EMAIL and attach it to the related record

Lokesh Pandey1
Tera Contributor

How can I create a PDF of the content of the triggered email i.e. subject and body and attach it to the related record. Ex:- An email is triggered from an Incident record  based on some conditions. Now, how can I capture that email in a PDF and attach it to the Incident record. 

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @Lokesh Pandey1 ,

What you can do is you can create a after Insert BR on sys_email table and you can pdf generator util like below to attach it to target record 

var html = "<p>"+current.subject+"</p>" +"<br>"+current.body;// you can fetch html body to pass on to the pdf

var result = new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDF(html, current.target_table, current.instance, 'My First PDF');
//body of pdf, table where you want to add the pdf, sys_id of the record where you want to add the generated pdf, name of file
;
action.setRedirect(current);

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

View solution in original post

3 REPLIES 3

Mohith Devatte
Tera Sage
Tera Sage

Hello @Lokesh Pandey1 ,

What you can do is you can create a after Insert BR on sys_email table and you can pdf generator util like below to attach it to target record 

var html = "<p>"+current.subject+"</p>" +"<br>"+current.body;// you can fetch html body to pass on to the pdf

var result = new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDF(html, current.target_table, current.instance, 'My First PDF');
//body of pdf, table where you want to add the pdf, sys_id of the record where you want to add the generated pdf, name of file
;
action.setRedirect(current);

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

Thanks a lot @Mohith Devatte .
This worked without any issues.

_Saqlain_
Tera Contributor

To Generate a PDF of the triggered EMAIL and attach it to the related record and also to email triggered

Create Before insert BR on sys_email table with script PFB 

 


if (current.headers && current.headers.indexOf('Sys_id of notification') !== -1) {  //sys of notification for which pdf needs to be generated
        var html = "<p>" + current.subject + "</p>" + "<br>" + current.body;



        var result = new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDF(html, current.target_table, current.instance, 'My First PDF');



        gs.log('Attach test' + result.attachment_id);
        var attchmt = new GlideRecord('sys_attachment');
        attchmt.addQuery('sys_id', result.attachment_id);
        attchmt.query();



        if (attchmt.next()) {
            var attcontent = new GlideSysAttachment().getContentStream(attchmt.sys_id);
            new GlideSysAttachment().writeContentStream(current, attchmt.getValue('file_name'), attchmt.getValue('content_type'), attcontent);





        }
    }

 

If you found this Information is useful, Please Accept as solution and hit like👍.

 

Thanks

Saqlain