- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 03:44 AM
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.
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.
@Anil Lande @Sandeep Rajput @Community Alums @Ratnakar7 @Chuck Tomasi @asifnoor @Sam Dey @Sam Dey1
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 06:30 AM
@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.
Here is how it looks when the data is filled into it using the script shared earlier.
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 06:30 AM
@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.
Here is how it looks when the data is filled into it using the script shared earlier.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 10:28 AM
Hi @Sandeep Rajput
can you please guide me on this to edit the email script?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 10:40 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 11:03 AM
(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);