How to edit the PDF attachmet with record details, while calling in the mail script.

Kunalo7o8
Tera Contributor

Hi ServiceNow Community,

I'm stuck on a requirement and need your help to achieve that.

 

I need to send an attachment using the email notification in ServiceNow,

while adding the attachment to the email body i.e.(Message HTML box) I need to auto-populate the user details on the related section of the PDF attachment.

below is the sample PDF attachment.

KunalWaghmare_2-1684664123386.png

can anyone have an idea how to achieve that?

I'm trying with the below email script to achieve that but it's not working.

KunalWaghmare_4-1684665192025.png

 

 

@Anil Lande @Sandeep Rajput @Community Alums @Ratnakar7 @Chuck Tomasi @asifnoor @Sam Dey @Sam Dey1 

Thank in advance.

 

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Kunalo7o8 Looks like you are not providing the correct field name for name field in the fillable PDF. I did some test runs to use this API in my background script and it works just fine for me.

 

Here is the sample script I used.

 

var list = new GlideRecord("incident");
list.addEncodedQuery("sys_id=e5cedec14776611092c98021336d438d"); //Change this with your incident sample record.
list.setLimit(1);
list.query();
while (list.next()){
    var attachment = new GlideRecord('sys_attachment');
    if(attachment.get('table_sys_id',list.getValue('sys_id'))){
        gs.info(attachment.getValue('file_name'));

        var pdfUtils = new global.GeneralPdfUtils();
        gs.info(pdfUtils.getPDFFields(attachment.sys_id));//Provides you the names of fields within PDF
        var jsonForPDF = '{"Check Box1":"true","Check Box2":"true","Address":"123 MG Road","Text6":"This is fillable","Text5":"PDF","Name":"Tom Hanks","Group6":"","Dropdown3":"","Dropdown2":"","Dropdown1":"","Check Box3":"false","Button7":"","Check Box4":"true"}';
        pdfUtils.prefillPdf(jsonForPDF,list.getValue('sys_id'),attachment.getValue('sys_id'),'incident','filled_pdf'+Math.random()+'.pdf');
        
        
    } 
       
}

 

In order to be sure about the field names present in the fillable PDF, use the following method.

pdfUtils.getPDFFields(attachment.sys_id); //Provides the list of fillable field names in a JSON format.

 

Here is how my fillable PDF looks when it is empty. 

Screenshot 2023-05-21 at 6.56.07 PM.png

Here is how it looks when the data is filled into it using the script shared earlier.

Screenshot 2023-05-21 at 6.57.08 PM.png

Attached is the fillable PDF for your reference. Test it by attaching it on any incident record and use the sys_id of that incident on line of two of this script.

list.addEncodedQuery("sys_id=e5cedec14776611092c98021336d438d"); //Change this with your incident sample record.

Hope this helps.

View solution in original post

9 REPLIES 9

Sandeep Rajput
Tera Patron
Tera Patron

@Kunalo7o8 Looks like you are not providing the correct field name for name field in the fillable PDF. I did some test runs to use this API in my background script and it works just fine for me.

 

Here is the sample script I used.

 

var list = new GlideRecord("incident");
list.addEncodedQuery("sys_id=e5cedec14776611092c98021336d438d"); //Change this with your incident sample record.
list.setLimit(1);
list.query();
while (list.next()){
    var attachment = new GlideRecord('sys_attachment');
    if(attachment.get('table_sys_id',list.getValue('sys_id'))){
        gs.info(attachment.getValue('file_name'));

        var pdfUtils = new global.GeneralPdfUtils();
        gs.info(pdfUtils.getPDFFields(attachment.sys_id));//Provides you the names of fields within PDF
        var jsonForPDF = '{"Check Box1":"true","Check Box2":"true","Address":"123 MG Road","Text6":"This is fillable","Text5":"PDF","Name":"Tom Hanks","Group6":"","Dropdown3":"","Dropdown2":"","Dropdown1":"","Check Box3":"false","Button7":"","Check Box4":"true"}';
        pdfUtils.prefillPdf(jsonForPDF,list.getValue('sys_id'),attachment.getValue('sys_id'),'incident','filled_pdf'+Math.random()+'.pdf');
        
        
    } 
       
}

 

In order to be sure about the field names present in the fillable PDF, use the following method.

pdfUtils.getPDFFields(attachment.sys_id); //Provides the list of fillable field names in a JSON format.

 

Here is how my fillable PDF looks when it is empty. 

Screenshot 2023-05-21 at 6.56.07 PM.png

Here is how it looks when the data is filled into it using the script shared earlier.

Screenshot 2023-05-21 at 6.57.08 PM.png

Attached is the fillable PDF for your reference. Test it by attaching it on any incident record and use the sys_id of that incident on line of two of this script.

list.addEncodedQuery("sys_id=e5cedec14776611092c98021336d438d"); //Change this with your incident sample record.

Hope this helps.

Hi @Sandeep Rajput 
can you please guide me on this to edit the email script?

@Kunalo7o8 As a first step use the following step to get to know the field names from the fillable PDF.

gs.info(pdfUtils.getPDFFields(attachment.sys_id)); //Provides the list of fillable field names in a JSON format.

In this method replace the attachment.sys_id with the sys_id of the attachment which is a fillable PDF.

Once you have the field names, it would be a lot easier to prepare the correct JSON for it. 

Kunalo7o8_0-1684692156615.png

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {

var gr = new GlideRecord('sys_attachment');
// gr.orderByDesc('sys_updated_on'); //sort with created descending
gr.addQuery('table_sys_id', current.sys_id);
gr.setLimit(1); // pick only 1 attachment record
gr.query();
if (gr.hasNext()) {
template.print("Attachment: " + "<br />");
while (gr.next()) {
gs.info(gr.getValue('file_name'));

var pdfUtils = new global.GeneralPdfUtils();
gs.info("the editable fields in the pdf = "+pdfUtils.getPDFFields(gr.sys_id)); //Provides you the names of fields within PDF
var jsonForPDF = '{"Name":"kunal","SSN#":"test","You were/will be laid off/discharged on":"123 MG Road"}';
var url = pdfUtils.prefillPdf(jsonForPDF, current.getValue('sys_id'), gr.getValue('sys_id'), 'incident', 'filled_pdf' + Math.random() + '.pdf');


// var url = gr.getTableName() + ".do?sys_id=" + gr.getValue('sys_id');
var attachLink = '<a href="' + url + '">' + gr.file_name + '</a>';
template.print(attachLink + "<br />");
}

template.print('<hr/>');
}


})(current, template, email, email_action, event);