Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Generate multiple PDF reports at once from List View

Srinika
Tera Contributor

I have a requirement to generate multiple PDF's at once from list of cases. Should be able to click on the checkboxes of the cases and add "Generate PDF" button.

In the PDF I want details from Case table and survey table. Can someone kindly help me on this requirement. 

 
1 ACCEPTED SOLUTION

Muhammad Salar
Giga Sage

Hello @Srinika ,

I have done this on knowledge articles table, you can modify this script as per your need. Try this

UI Action:

MuhammadSalar_0-1761147117096.png
UI Action script:

function test() {
    var sysId = g_list.getChecked();
    var a = new Array();
    a = sysId.split(",");
    var b = a.length;
   
    var ga = new GlideAjax('GetKnowledgeArticleBody');
    ga.addParam('sysparm_name', 'getHtml');
    ga.addParam('sys_id', a.toString());
    ga.getXML(getRequired);

    function getRequired(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var json = JSON.parse(answer);

        for (var i = 0; i < json.length; i++) {
            window.open('/sys_attachment.do?sys_id=' + json[i].id);
        }
    }
     window.setTimeout(fun2, 3000);

    function fun2() {
        var att = new GlideRecord('sys_attachment');
        att.addQuery('table_name', 'kb_knowledge');
        att.addQuery('table_sys_id', 'IN', sysId);
        att.orderByDesc('sys_created_on');
        att.setLimit(b);
        att.query();
        while (att.next()) {
            att.deleteRecord();
        }
    }
}
I have used fun2 for deleting attachments from the record after downloading, if you don't want it, remove it.

Script include function:
getHtml: function() {
        var hfInfo = new Object();
        hfInfo["FooterText"] = "Test";
        hfInfo["PageSize"] = "A4";
        hfInfo["GeneratePageNumber"] = "true";
        hfInfo["TopOrBottomMargin"] = "60";
        hfInfo["LeftOrRightMargin"] = "42";
        hfInfo["PageOrientation"] = "PORTRAIT";

        var list = [];
        var sysId = this.getParameter('sys_id');
        var gr = new GlideRecord('kb_knowledge');
        gr.addQuery('sys_id', 'IN', sysId.toString());
        gr.query();
        while (gr.next()) {
            var object = {};
            var name = gr.getDisplayValue('short_description');
            var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
           
            var result = v.convertToPDFWithHeaderFooter('<h1>' + gr.getDisplayValue('short_description') + '</h1>' + '<p style="margin-top:-20px;">' + gr.getDisplayValue('number') + '<b>' + ' . ' + '</b>' + gr.getDisplayValue('version.version') + '<b>' + ' . ' + '</b>' + gr.published + '</p>' + '</p>' + '<div style="margin-top:-30px;">' + gr.getValue("text") + '</div>', 'kb_knowledge', gr.getUniqueValue(), name, hfInfo);

            object.id = result.attachment_id;
            list.push(object);

        }
        return JSON.stringify(list);

    },
Used PDFGenerationAPI to attach pdf to record.


 

View solution in original post

5 REPLIES 5

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Srinika 
Did you tried right click on the column and export as PDF?
This is the only way else you need to go for Custom Solution

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi Gaurav,

 

I want a custom solution by adding a button on the List view, I should be able to generate PDF for the selected cases. May be for 5 cases at a time. 

Muhammad Salar
Giga Sage

Hello @Srinika ,

I have done this on knowledge articles table, you can modify this script as per your need. Try this

UI Action:

MuhammadSalar_0-1761147117096.png
UI Action script:

function test() {
    var sysId = g_list.getChecked();
    var a = new Array();
    a = sysId.split(",");
    var b = a.length;
   
    var ga = new GlideAjax('GetKnowledgeArticleBody');
    ga.addParam('sysparm_name', 'getHtml');
    ga.addParam('sys_id', a.toString());
    ga.getXML(getRequired);

    function getRequired(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var json = JSON.parse(answer);

        for (var i = 0; i < json.length; i++) {
            window.open('/sys_attachment.do?sys_id=' + json[i].id);
        }
    }
     window.setTimeout(fun2, 3000);

    function fun2() {
        var att = new GlideRecord('sys_attachment');
        att.addQuery('table_name', 'kb_knowledge');
        att.addQuery('table_sys_id', 'IN', sysId);
        att.orderByDesc('sys_created_on');
        att.setLimit(b);
        att.query();
        while (att.next()) {
            att.deleteRecord();
        }
    }
}
I have used fun2 for deleting attachments from the record after downloading, if you don't want it, remove it.

Script include function:
getHtml: function() {
        var hfInfo = new Object();
        hfInfo["FooterText"] = "Test";
        hfInfo["PageSize"] = "A4";
        hfInfo["GeneratePageNumber"] = "true";
        hfInfo["TopOrBottomMargin"] = "60";
        hfInfo["LeftOrRightMargin"] = "42";
        hfInfo["PageOrientation"] = "PORTRAIT";

        var list = [];
        var sysId = this.getParameter('sys_id');
        var gr = new GlideRecord('kb_knowledge');
        gr.addQuery('sys_id', 'IN', sysId.toString());
        gr.query();
        while (gr.next()) {
            var object = {};
            var name = gr.getDisplayValue('short_description');
            var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
           
            var result = v.convertToPDFWithHeaderFooter('<h1>' + gr.getDisplayValue('short_description') + '</h1>' + '<p style="margin-top:-20px;">' + gr.getDisplayValue('number') + '<b>' + ' . ' + '</b>' + gr.getDisplayValue('version.version') + '<b>' + ' . ' + '</b>' + gr.published + '</p>' + '</p>' + '<div style="margin-top:-30px;">' + gr.getValue("text") + '</div>', 'kb_knowledge', gr.getUniqueValue(), name, hfInfo);

            object.id = result.attachment_id;
            list.push(object);

        }
        return JSON.stringify(list);

    },
Used PDFGenerationAPI to attach pdf to record.


 

Hi @Muhammad Salar 

 

Thank you so much... this helped