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 I'm trying with the above edits can you please check once? why I'm getting the null?

 

@Kunalo7o8 Looks like the PDF you are using is a non fillable PDF, can you test your script with the PDF attached with this post.

hi @Sandeep Rajput 

@Sam Dey1  can you guys please tell me about the fillable pdf I did some research on that but couldn't find anything is it feature in servicenow or anything else.

 

Please tell me.

@Sandeep Rajput I tried it from my background script but still it shows me an error.

Kunalo7o8_0-1684695042053.png

 

Sam Dey1
Kilo Guru

Hi Kunal, 

 

From the PDF screenshot you have shared it seems that is a non-fillable pdf. So to automate the pdf fill- you have do define a pdf file where you name the fields that you want to populate, for e.g. - 

First create a PDF where you put a field name called "name" and that field is a text box. then you run the api to fill-up the field called name it should do the work. 
So essentially you have to create a fillable pdf first