Document Templates auto-generate a PDF and attach to case

AijaRe
Kilo Sage

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.

1 ACCEPTED SOLUTION

AijaRe
Kilo Sage

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!

View solution in original post

11 REPLIES 11

Chandana E S
Tera Contributor

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.

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.

AijaRe_0-1744192929349.pngAijaRe_1-1744192958588.png

Then you can use the inputs in the script step:

outputs.generatedattachmentid = new sn_doc.GenerateDocumentAPI().generateDocumentForTask(inputs.targetRecordId, inputs.documentTemplateId, inputs.pdfName);

Chandana E S
Tera Contributor

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?

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.

Hi,

Can you please share the exact steps for the agent to edit the document before generating the document in the workspace?