Document generation Api works from background script but not through HR flow or BR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I have tested the document generation through background script -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Hi @Veena-EeshB ,
You can first try testing document generation using the Automatic Document Task Creation subflow (3D5b399625c7320010296ad3de17c260b7). If it works successfully, you can then try implementing the same approach in the Business Rule.
This will help us identify whether the issue is related to the Business Rule execution or the document generation process itself.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago - last edited 2 weeks ago
Hi @Veena-EeshB,
If you are passing record sys id dynamically, try current.getUniqueValue() .
I have implemented this in a Business rule and it works for me.
var result = new sn_doc.GenerateDocumentAPI().generateDocumentForTask(
current.getUniqueValue(), // where to attach
"b28434fd9f4bc310092d88d86924abbd", // html template
documentName // file name
);
Edit: Use after or async BR to generate attachment and attach it on the record.
If this response resolved your issue, please consider marking it as Helpful and accepting it as the Solution. Your feedback helps other ServiceNow Community members find reliable answers.
Kind Regards,
Ehab Pilloor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Hello @Veena-EeshB
Since the same call works from a background script but not from a Business Rule or Flow, I would suspect timing and context rather than the API itself. A background script runs after the record already exists, but a Business Rule or Flow often fires before the record is fully committed, or runs as a user or scope that cannot see the template or the record.
Start with the timing, since it is the most common cause.
- If your Business Rule is “before” insert or update, change it to “after” so the record exists in the database.
- Make sure the condition actually passes, and add gs.info() lines before and after the call to confirm the rule fires.
- Log the return value of generateDocumentForTask and print it, because it can fail quietly without throwing an error.
- Check System Logs > System Log > All for messages from sn_doc around the same time.
Next, look at who runs it and whether the record is the right type. The API is built for task-based records, so the record in recordId must be on a table that extends Task, such as sn_hr_core_case or sn_hr_core_task.
- Confirm the template’s Table field matches the table of the record you pass in.
- Check that the user running the rule (or the flow’s “Run As” user) has the sn_doc roles, plus access to the template and the record.
- In the Flow, set Run As to System User, or to a user you know can generate the document.
- Open the Flow execution and check the action’s output values, not just its status, since “Completed” does not mean a document was created.
Then verify where the file ends up. The generated PDF is saved as an attachment on the record, so check the Attachments on the target record, and also look at the Generated Documents table (sn_doc_generated_document, though I am not certain of the exact name in your release).
If the rule is “after” and the user is correct, try wrapping the call in a Script Include with gs.eventQueue, or run it asynchronously with an “async” Business Rule. This avoids conflicts with the record’s own transaction.
Since the background script works, I would test it once as the same user the rule runs as, using Impersonate, to isolate a permissions problem. If you can share your release, the table the record is on, and what the log shows, I can narrow it down further.