Automate the form variables population in a custom template and attach it to the RITM

Nagashree5
Tera Contributor

Hi All,

 

I have created a custom scope in which I have created two tables- SDRequests and SDTasks.

Requirement : Once a request is submitted through the portal, the form-level variables must be automatically populated in to a customized template and attach to the submitted request.

Suppose the form has three variables - Requested for, Company and Date. Once, these fields are filled and form is submitted, these variables should be populated in a custom pdf like below.

Dear (Requested for),

I'm working for (Company) till (Date)

It should automatically fill these details based on the request and get attached to post submission.

Is this feasible in Servicenow.

Can anyone please guide me on this.

Thank you in Advance.

1 ACCEPTED SOLUTION

Siva Jyothi M
Mega Sage

Hi @Nagashree5 ,

Can you try the below script.

var fileName = 'Req_' + current.number;
var pageProperties = {
	HeaderImageAttachmentId:'',
HeaderImageAlignment: 'LEFT', 
PageSize: 'A4',
GeneratePageNumber: 'true',
TopOrBottomMargin: '72',
LeftOrRightMargin: '36'
};
var html = '<html>' +
'<head>' +
'<title></title>' +
'</head>' +
'<body>' +
'<h2>The New Request</h2>' +
'<p>Header of the Template</p>' +
'<ul>' +
'<li>Open on behalf of this user: '+ current.variables.open_on_behalf_of_this_user.getDisplayValue() + '</li>' + 
'<li>Please identify what you require: ' + current.variables.please_identify + '</li>' + 
'</ul>' +
'<p>Authorised approver:<br />' +
current.variables.authorised_approver + '</p>' +
'<p>&nbsp;</p>' + 
	'</body>' + 
	'</html>';
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDFWithHeaderFooter(html, 'sc_req_item', current.sys_id, fileName, pageProperties, ''); 
result = JSON.stringify(pdf);

Mark this answer as correct and helpful if it solves your issue.

 

Regards,

Siva Jyothi M.

View solution in original post

4 REPLIES 4

Ravi Gaurav
Giga Sage
Giga Sage

Yes, this is feasible in ServiceNow. Here is an overview of the steps you can follow to achieve this:

Create a custom PDF template with the desired formatting and placeholders for the form-level variables. You can use the HTML to PDF feature in ServiceNow to create the PDF.

Add a business rule on the SDRequests table that triggers on insert and update. In the script of the business rule, retrieve the form-level variables from the request record and use them to populate the placeholders in the PDF template.

Use the ServiceNow API to attach the PDF to the request record. You can use the GlideSysAttachment class to create the attachment and attach it to the request record.

Here is some sample code that you can use as a starting point for the business rule:

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

// Retrieve the form-level variables from the request record
var requestedFor = current.getValue('requested_for');
var company = current.getValue('company');
var date = current.getValue('date');

// Retrieve the PDF template and replace the placeholders with the form-level variables
var pdfTemplate = gs.getProperty('my.pdf.template');
pdfTemplate = pdfTemplate.replace('(Requested for)', requestedFor);
pdfTemplate = pdfTemplate.replace('(Company)', company);
pdfTemplate = pdfTemplate.replace('(Date)', date);

// Convert the HTML to PDF
var pdf = new global.GeneraPDF();
pdf.setHtml(pdfTemplate);
pdf.generate();

// Create an attachment record and attach the PDF to the request record
var attachment = new global.GlideSysAttachment();
attachment.write(current, 'My PDF Attachment', 'application/pdf', pdf.getBytes());
attachment.setContentType('application/pdf');
attachment.setFileName('My PDF Attachment.pdf');
attachment.setTableName('sd_request');
attachment.setTableSysId(current.getUniqueValue());
attachment.setCreatedBy(current.sys_created_by);
attachment.insert();

})(current, previous);

Note that you will need to adjust the code to match the specific field names and table names in your ServiceNow instance, and to include the correct PDF template and attachment details.


If my answer solved your issue, please mark my answer as ‌‌ Correct & ‌‌Helpful based on the Impact.

 

Thanks

Ravi Gaurav

 

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


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 @Ravi Gaurav ,

 

Thank you for the response.

Could you please elaborate on creating a pdf from HTML. I'm very new to this pdf generating thing.

Thank you in Advance.

Hi @Ravi Gaurav ,

 

Could you please elaborate on the above reply. I'm new to this pdf generating topic. Can you please guide me on this.

 

Thank you in Advance.

Siva Jyothi M
Mega Sage

Hi @Nagashree5 ,

Can you try the below script.

var fileName = 'Req_' + current.number;
var pageProperties = {
	HeaderImageAttachmentId:'',
HeaderImageAlignment: 'LEFT', 
PageSize: 'A4',
GeneratePageNumber: 'true',
TopOrBottomMargin: '72',
LeftOrRightMargin: '36'
};
var html = '<html>' +
'<head>' +
'<title></title>' +
'</head>' +
'<body>' +
'<h2>The New Request</h2>' +
'<p>Header of the Template</p>' +
'<ul>' +
'<li>Open on behalf of this user: '+ current.variables.open_on_behalf_of_this_user.getDisplayValue() + '</li>' + 
'<li>Please identify what you require: ' + current.variables.please_identify + '</li>' + 
'</ul>' +
'<p>Authorised approver:<br />' +
current.variables.authorised_approver + '</p>' +
'<p>&nbsp;</p>' + 
	'</body>' + 
	'</html>';
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDFWithHeaderFooter(html, 'sc_req_item', current.sys_id, fileName, pageProperties, ''); 
result = JSON.stringify(pdf);

Mark this answer as correct and helpful if it solves your issue.

 

Regards,

Siva Jyothi M.