We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Auto generate PDF of RITM with related list and attach to the RITM itself

mr18
Tera Guru

I want to auto generate a PDF whenever the request is completed and auto attach the PDF to the actual RITM.

The PDF should contain all variable information along with the Approver related list

18 REPLIES 18

Aditya_hublikar
Mega Sage

Hello @mr18 ,

 

This can acheived using After update business rule .

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

    // Add your code here
    var v = new sn_pdfgeneratorutils.PDFGenerationAPI();

    var gr = new GlideRecord("sc_req_item");
    var html = "";

    if (gr.get(current.sys_id)) {
        var ga = new GlideRecord('sysapproval_approver');
        var app = [];
        ga.addQuery('sysapproval', current.sys_id);
        ga.query();
        while (ga.next()) {
            app.push(ga.getDisplayValue('approver'));
        }

        html += "<h2>RITM Details</h2>";
        html += "<b>RITM Number:</b> " + gr.number + "<br/>";
        html += "<b>Requested For:</b> " + gr.getDisplayValue('requested_for') + "<br/>";
        html += "<b>Requested By:</b> " + gr.getDisplayValue('opened_by') + "<br/>";
        html += "<b>Catalog Item:</b> " + gr.getDisplayValue('cat_item') + "<br/>";
        html += "<b>State:</b> " + gr.getDisplayValue('state') + "<br/>";
        html += "<b>Priority:</b> " + gr.getDisplayValue('priority') + "<br/>";
        html += "<b>Assignment Group:</b> " + gr.getDisplayValue('assignment_group') + "<br/>";
        html += "<b>Assigned To:</b> " + gr.getDisplayValue('assigned_to') + "<br/>";
        html += "<b>Short Description:</b> " + gr.short_description + "<br/>";
        html += "<b>Description:</b> " + gr.description + "<br/>";
        html += "<b>Approval List:</b> " + 
        (app.length > 0 ? app.join(', ') : 'No Approvers') 
        + "<br/>";


    }

    var result = v.convertToPDF(html, "sc_req_item", current.sys_id, "RITM_PDF");

    gs.info(JSON.stringify(result));


})(current, previous);

 

Aditya_hublikar_0-1771166690046.pngAditya_hublikar_1-1771166706518.png

 

If you want to some styling   so you can also add that using css .

 

If this helps you then you can mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant

Hello @mr18 ,

 

 I hope you are doing well . Does my response helps you ?

 

If my response helps you then mark it as helpful and accept as solution

Regards,

Aditya ,

Technical Consultant

Thank you @Aditya_hublikar  for the reply but this code only pulls RITM information.

I want all variables as well as Approval Related list information as well

Hello @mr18 ,

 

    var ga = new GlideRecord('sysapproval_approver');
        var app = [];
        ga.addQuery('sysapproval', current.sys_id);
        ga.query();
        while (ga.next()) {
            app.push(ga.getDisplayValue('approver'));
        }

 

This code is getting details of approvers from approver list.

Hello @mr18 ,

 

This above code is already mentioned in previous one and you can also modify some code as per requirnment . You can also fetch variable details using current.variables.your_variable_name in that business rule