The CreatorCon Call for Content is officially open! Get started here.

PDF generation from record

rah dev
Tera Contributor

Hi community, good day,
I am working on a requirement.
On the incident table, I need a button so that when I click that button, the record gets downloaded in PDF format. But this is not happening.
I am using this approach...

rahdev_0-1759410201707.png

rahdev_1-1759410231869.png

function onClickGeneratePDF() {
    var ga = new GlideAjax('global.IncidentPDFGenerator');
    ga.addParam('sysparm_name', 'generatePDF');
    ga.addParam('sys_id', g_form.getUniqueValue());
    ga.getXMLAnswer(function(response) {
        if (response) {
            window.open(response, "_blank");
        } else {
            alert("Failed to generate PDF.");
        }
    });
}
Script include:
rahdev_2-1759410294207.png
var IncidentPDFGenerator = Class.create();
IncidentPDFGenerator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    generatePDF: function() {
        var sysId = this.getParameter('sys_id');
        var gr = new GlideRecord('incident');
        if (!gr.get(sysId)) {
            return '';
        }

        // Collect fields
        var number = gr.getValue('number');
        var assignedTo = gr.getDisplayValue('assigned_to');
        var state = gr.getDisplayValue('state');

        // Prepare content (simple text for PDF)
        var content = "Incident Details\n\n";
        content += "Number: " + number + "\n";
        content += "Assigned To: " + assignedTo + "\n";
        content += "State: " + state + "\n";

        // Save as attachment
        var fileName = "Incident_" + number + ".pdf";
        var sa = new GlideSysAttachment();
        var attSysId = sa.write('incident', sysId, fileName, content);

        // Return a download URL
        return gs.getProperty('glide.servlet.uri') + "sys_attachment.do?sys_id=" + attSysId;
    }

});
1 ACCEPTED SOLUTION

Just realized close bracket is missing

function onClickGeneratePDF(){
  window.open("/incident.do?PDF&sys_id=" + g_form.getUniqueValue()+ "&sysparm_view=default", "_blank");
}
Thank you,
Palani

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@rah dev 

you can use the easy approach shared by @palanikumar 

You need not use script include then.

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

kaushal_snow
Mega Sage

@rah dev ,

 

Use ServiceNow PDFGenerationAPI (sn_pdfgeneratorutils.PDFGenerationAPI) on the server side to convert HTML or a Document Template into a proper PDF (not just raw text), attach it to the record, and return a download URL, then call that via Client Script or UI Action....


For example, your Script Include (or server logic) would build an HTML or template view, pass it to PDFGenerationAPI to generate the PDF, write it as an attachment, and return the sys_id or URL. The client script would still call via GlideAjax / UI Action and open the returned URL for download...Also double check that your Script Include is client callable (extends AbstractAjaxProcessor) and in the correct scope so that your client script can invoke it...

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/