Sending automatically generated pdf via email

Aditya Bashyam
Tera Expert

How can I automatically generate a PDF with requested item details when the state changes to "Closed Complete" and automatically send it via email?

1 ACCEPTED SOLUTION

VasukiS
Tera Guru

Hi @Aditya Bashyam 

 

I just tried the background script below, and it successfully created an attachment in the specified RITM.

You can create a Business Rule with the condition set to trigger when the state changes to 'Closed Complete'. Add similar code to the Business Rule, and it will attach the PDF to the RITM ticket. This makes it easy to include the attachment in notifications.

 

 
var requestedItemSysId = '4279ab84833452103bd3cfc0deaad308'; // Replace with your requested item sys_id
var gr = new GlideRecord('sc_req_item');
if (gr.get(requestedItemSysId)) {
    var html = '<h1>Requested Item Details</h1>';
    html += '<p><strong>Number:</strong> ' + gr.number + '</p>';
    html += '<p><strong>Short Description:</strong> ' + gr.short_description + '</p>';
    html += '<p><strong>Description:</strong> ' + gr.description + '</p>';
    // Add more fields as needed

    var pdfAPI = new sn_pdfgeneratorutils.PDFGenerationAPI();
    var pdfSysId = pdfAPI.convertToPDF(html, 'sc_req_item', requestedItemSysId, 'Requested_Item_Details.pdf');
   
    if (pdfSysId) {
        // Attach the PDF to the record
        var attachment = new GlideSysAttachment();
        attachment.write('sc_req_item', requestedItemSysId, 'Requested_Item_Details.pdf', pdfSysId);
        gs.log('PDF generated and attached successfully.');
    } else {
        gs.error('PDF generation failed.');
    }
} else {
    gs.error('Requested item not found.');
}

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Aditya Bashyam 

do you want PDF to be generated with the RITM variables and send it over email?

you can use PDF Generation API and then attach and then send email.

check this link which has script from Piyush

Generate PDF file for catalog variables and attach it to RITM 

you can check the PDF Generation API for this

Generating Custom PDFs - using the new PDFGenerationAPI

another method

Generate PDF and attach to the record

When catalog item is submitted, attach variables to RITM as a PDF 

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