Notification

komalkp
Tera Contributor

when sending a notification with a pdf the pdf should be attached to the notification so that the user receive it.

 

(function executeRule(current, previous /*null when async*/) {

    // Only run when state changes to Resolved or Closed
    if (!(current.state.changesTo('6') || current.state.changesTo('7'))) {
        return;
    }
 
    var pdf = new GlidePDFDocument("Incident_" + current.number + ".pdf");
    pdf.addText("Incident Details", 18);
    pdf.addText("Number: " + current.number);
    pdf.addText("Short Description: " + current.short_description);
    pdf.addText("Resolution Notes: " + current.close_notes);
    pdf.addText("Resolution Code: " + current.close_code); // Change field name if different
 
    var bytes = pdf.getBytes();
 
    var attachment = new GlideSysAttachment();
    attachment.write(current, "Incident_" + current.number + ".pdf", "application/pdf", bytes);
})(current, previous);
 
this is the code .i am not able to open the pdf , while opening the pdf i am getting error "failed to load the document"
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@komalkp 

so you want to attach PDF to record and then send email and that email should have attachment?

If yes then do this

1) you should check this link on how to add PDF to record via after update business rule, business rule should have correct notification trigger condition

Generate PDF and attach to the record 

2) then use gs.eventQueue() to trigger the event and associate this event with notification

3) in the notification ensure "Include attachments" is checked

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Robert H
Mega Sage

Hello @komalkp ,

 

Please use the following code instead. It takes care of generating the PDF and attaching it at the same time.

Adjust the content of "html" as per your requirements.

 

var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
var pdfName = "Incident_" + current.number + ".pdf";
	
var html = current.getValue('short_description');

var result = v.convertToPDF(html, "incident", current.getUniqueValue(), pdfName);

 

You can learn more about this PDF API here.

 

Regards,

Robert