- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2024 08:19 AM - edited ‎09-02-2024 10:16 AM
Hello!
I have a requirement to auto-generate and attach a PDF document to an HR case once the Record Producer is submitted.
I was able to achieve that with HR Document Templates by creating a Flow that would look up PDF draft (created by leveraging 'Automatically Create Draft Document' Case Option) -> Look up attachment where this draft record is referenced -> Copy attachment to the case record.
I would like to migrate this logic to Document Templates since HR Document Templates are being deprecated.
For other reasons, as a Type, I'd like to use HTML Template.
Can you please suggest how it would be possible to implement automatic document generation using Document Templates?
I checked the Generate PDF Document Action in Flow Designer, but that seems to work only for Tasks which my case does not have.
The 'Automatically Create Draft Document' Case Option seems useless with Document Templates and I haven't found the logic how to move the documents 'behind the scenes'. I know that for Document Templates there is the option 'Automatically Initiate Document tasks', but again seems to be connected to tasks.. How can I trigger document generation using HTML documents from Document Templates?
Any tips appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 05:58 AM
The issue was solved by using this API:
//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 generated
new sn_doc.GenerateDocumentAPI().generateDocumentForTask(recordId, documentTemplateId, pdfName);
as mentioned in SN Documentation: Document generation methods (servicenow.com)
I created a custom Flow Action very similar to Generate PDF Template that uses the API mentioned above and called it for the HR Case, filling the fields with Case Sys ID and Document Template Sys ID.
Also checked that it works both for PDF and HTML documents created with Document Templates.
This is how the process would look like in a business rule (with condition Document Template is Not Empty):
(function executeRule(current, previous /*null when async*/ ) {
var templateSysId = current.document_template.sys_id;
try {
var pdfName = current.document_template.name + "-" + current.subject_person.name;
var pdfSysId = new sn_doc.GenerateDocumentAPI().generateDocumentForTask(current.getUniqueValue(), templateSysId, pdfName);
} catch (e) {
gs.error("Error message: " + e.message);
}
})(current, previous);
After executing the script, a PDF is generated and attached to the case.
The logs still show an error message "Document task not found", but this is the cleanest solution I've found so far. If someone has a better suggestion, please share!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2025 03:51 AM
Hi,
This is super helpful. I tried with the Business rule and it works fine but with the custom action it is not working. Any idea?
I have created the action in HR Core scope.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-09-2025 03:04 AM
Hi @Chandana E S , thanks, glad it helped!
I created document generation action in Document Template scope, this way it's also flexible for anyone to use it.
Check RCAs, maybe you need to allow some (although I don't think it was needed).
And check if inputs are configured both in Inputs and Script step.
Then you can use the inputs in the script step:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2025 12:29 AM
Hello,
I changed the Run As session user on the flow designer and it started working fine without any scope issues. Thanks for your response.
One more question though, is there any possibility to edit these attached documents as in case of the Edit Document option using Document template on the HR service?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2025 08:37 AM
You mean Editing Document after it has been created and attached to the case?
I don't think you can edit a document after creation in any case. Agent can Edit document in Workspace before generating it.
What exactly is your requirement for this situation? Editing the document after creating it would beat the purpose of document auto-generation.
We had a requirement for the Agent to add information to the document in particular cases, so we created a variable in Record Producer that would tell us weather the Agent involvement is needed or not. If it was not needed, the document gets auto-generated, but if the document needs to be edited, a case gets created and agent can work on it and edit the document.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-02-2025 09:40 AM
Hi,
Can you please share the exact steps for the agent to edit the document before generating the document in the workspace?