The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Ankur Bawiskar
Tera Patron
Tera Patron

Often, we need to configure a document template for a table and generate a PDF file to attach to the target record using server-side scripting.

Here's how you can generate and attach the document template file to the target record. This works in both global & scoped app.

//recordId - Sys ID of the target record. This record must be from the table defined in the document template.
//documentTemplateId - Sys ID of the document template to use for pdf generation.
//pdfName - Name of the pdf file
var recordId = 'a9a16740c61122760004fe9095b7ddca';
var documentTemplateId = '50e6c385db175e10727ee7dcd39619f2';
var pdfName = 'INC_Template.pdf';
new sn_doc.GenerateDocumentAPI().generateDocumentForTask(recordId, documentTemplateId, pdfName);

Output:

generate document using document template from server side script.gif

Comments
ShahidaM
Tera Contributor

When I created document template for Request table and try to run this background script.the pdf document shows with html tags it doesnot show in right format.Can someone help me what is the issue here?

Vedavalli
Tera Expert

Hi @Ankur Bawiskar 

for suppose in document template (table is incident)
using background script

var recordId = 'a9a16740c61122760004fe9095b7ddca';(sys_id of the problem which is having incident as parent)
var documentTemplateId = '50e6c385db175e10727ee7dcd39619f2';
var pdfName = 'INC_Template.pdf';
new sn_doc.GenerateDocumentAPI().generateDocumentForTask(recordId, documentTemplateId, pdfName);
can we generate document for problem record

 

Ankur Bawiskar
Tera Patron
Tera Patron

it should be on same table i.e. Document template (incident) and Target Record also same

I haven't tested when both are different.

ShahidaM
Tera Contributor

How can we use it for RITM and send email to user with pdf.

ShahidaM
Tera Contributor


@Ankur Bawiskar can you help how to use this for RITM and send email to user with these pdf.

I am using below background script to generate 2 pdfs

(function executeRule(current, previous /*null when async*/) {
 
    // Define the document template IDs
    var templateId1 = '553401f31bb96a14fcb82f00604bcb3c'; // First document template sys_id
    var templateId2 = 'cd0fc9c01b8e2e14fcb82f00604bcb67'; // Second document template sys_id
 var recordid='4b1434e787462e907b8377b2debb352d';
    // PDF filenames
    var pdfName1 = 'Employee_Verification_Letter.pdf';
    var pdfName2 = 'Host_Business_Trip.pdf';
 
    // Generate and attach first PDF
    new sn_doc.GenerateDocumentAPI().generateDocumentForTask(recordid, templateId1, pdfName1);
 
    // Generate and attach second PDF
    new sn_doc.GenerateDocumentAPI().generateDocumentForTask(recordid, templateId2, pdfName2);
 
})();
 
it is working but when I use in flow designer or business rule it is not working.Please help me on this.
ashish781
Tera Explorer

Hi @Ankur Bawiskar , Actually I'm using the same functionality in the flow designer i.e. created input variables, script step used the same script passed the input values and mapped it. Getting the output , but the issue is it is not working as system user , the generated record id comes as Blank.  

Version history
Last update:
‎01-23-2025 09:00 AM
Updated by:
Contributors