RecreatePdfAPI
RecreatePdfApi: Description
The class RecreatePdfApi can be used to regenerate the PDF file of invoices or dunnings via Salesforce flows, own Apex code, or a REST endpoint.
Use this when a finalized record's PDF must be refreshed without changing any invoice or dunning data – for example, after a template change, or as the post-processing step that follows Avalara response mapping.
PDF recreation concepts
Usually, JustOn Billing & Invoice Management generates a PDF file when finalizing an invoice. In certain business use cases, you may need to recreate the PDF file for an already finalized invoice (status Open). Doing so does not modify any invoice data. The same applies to dunnings in status Closed.
When recreating the PDF, JustOn Billing & Invoice Management will
- clear the
PDF__cfield on the validated records synchronously - start a
PdfGeneratebatch chain that regenerates the PDF for each record - generate a fresh
ContentVersionrather than reuse an existing one
Regenerating the PDF does not constitute a suitable means to correct an invoice. For details, see Invoice Correction.
For the corresponding manual operation in the user interface, see Recreating Invoice PDF.
Invocable Method
| API Name | Label | Description |
|---|---|---|
| recreatePdf | Recreate PDF | Recreates the PDF for a list of invoices or dunnings |
The method is exposed as an invocable action (Recreate PDF) for use in flows, as a static Apex method, and via a REST endpoint.
Limitations
The records passed to a single call must meet the following criteria:
- All record IDs must be of the same SObject type. Mixing
Invoice__candDunning__cin a single call is rejected. - Only
Invoice__candDunning__crecords are supported. -
Records must have an allowed status:
- For invoices:
Open,Paid,Settled,Canceled,Closed - For dunnings:
Closed
- For invoices:
Request Parameters
| API Name | Required | Type | Label | Description |
|---|---|---|---|---|
| recordIds | List | Record IDs | The references to the ID field of the invoices or dunnings whose PDF is to be regenerated All IDs must be of the same SObject type. The list is de-duplicated, and null entries are dropped before processing. |
Response Parameters
The method returns a List<Id> of chain IDs. A single chain processes the whole input list, so every element of the returned list holds the same chain ID – Salesforce's invocable contract requires the output list size to equal the input list size. Direct Apex callers typically just read the first element.
| API Name | Type | Label | Description |
|---|---|---|---|
| chainId | Id | Chain Id | The ID of the BatchJobChain__c record that tracks the batch chain. Use it to poll progress.The value is null if none of the passed records exist in the database. |
Error Handling
Apex and Invocable Method
The API may throw ONB2.Exceptions.InvalidParameterException with one of the following messages:
| Message | Description Next Steps |
|---|---|
recordIds must not be null. |
The passed list is null.Pass a non-empty list of record IDs. |
recordIds must not be empty. |
The passed list is empty after removing null entries.Pass a non-empty list of record IDs. |
All recordIds must be of the same type. Found both X and Y. |
The list mixes different SObject types. Split the call into one call per SObject type. |
Only Invoice__c and Dunning__c are supported. Got: X. |
The list contains IDs of an unsupported SObject type. Only pass Invoice__c or Dunning__c IDs. |
Record X has status "Y" which is not allowed for PDF recreation. Allowed: {...}. |
A record has a status outside the allowed set. Restrict the input to records in an allowed status. |
REST Endpoint
Over the REST endpoint, error conditions are translated into HTTP responses as follows:
| HTTP Status | Condition | Response Body |
|---|---|---|
200 |
Success, or all passed IDs are well-formed but do not match any existing record | Chain ID as a JSON string, or null if no records exist |
400 |
One of the recordIds is not a structurally valid Salesforce ID |
{"errorCode":"INVALID_ID","message":"..."} |
400 |
InvalidParameterException (null/empty list, mixed types, unsupported type, disallowed status) |
{"errorCode":"INVALID_PARAMETER","message":"..."} |
500 |
Any other unexpected error (for example, Salesforce async limits or DML errors when enqueuing the chain) | Platform default error body |
Info
In all error cases, no records are modified – the transaction is rolled back, and the original PDF__c values remain.
Well-formed but non-existing IDs are not treated as an error. They are silently dropped, and the REST endpoint returns HTTP 200 with null if no records remain after filtering.
RecreatePdfApi: Example Use Cases
Using Salesforce Flow
Assume the following use case: You want to regenerate the PDF of an invoice whenever the field EmailTemplate__c is updated, so that the latest template content is reflected in the document. Now you can use this field modification as a condition to trigger a Salesforce flow that calls the Recreate PDF action.
You can set up the flow as follows:
| Flow Element | Option | Value |
|---|---|---|
| Start | Object | Invoice (ONB2__Invoice__c) |
| Trigger | A record is updated |
|
| Conditions | All Conditions Are Met (AND)ONB2__EmailTemplate__c Is Changed True |
|
| Run | Only when a record is updated to meet the condition requirements |
|
| Action | Action | Recreate PDF |
| Input Values | Record IDs: {!$Record.Id} |
Depending on your specific use case, the flow setup will vary. The trigger object can also be Dunning (ONB2__Dunning__c).
Using Apex Code
Assume your business has developed an own integration with JustOn Billing & Invoice Management that updates invoice data. Now you need a piece of Apex code that calls the class RecreatePdfApi after the update, like, for example
public class CustomRecreatePdf {
public void executeRecreatePdf(List<Id> recordIds) {
Id chainId = ONB2.RecreatePdfApi.recreatePdf(recordIds)[0];
// chainId can be used to poll BatchJobChain__c for progress.
// chainId is null if none of the passed IDs match an existing record.
}
}
Using REST
JustOn Billing & Invoice Management also accepts an HTTP POST request at the following endpoint:
https://INSTANCE.salesforce.com/services/apexrest/RecreatePdfApi
Replace INSTANCE with the actual Salesforce instance or custom domain where JustOn is installed.
The body of the request must be in JSON format and contain a list of invoice or dunning IDs:
{"recordIds":["id1","id2"]}
A successful call returns HTTP status code 200 and the chain ID in the response body:
"a0v5000000xYzAaAAB"
For details about authentication and possible HTTP status codes, see Step Two: Set Up Authorization and RestResponse Class in the Salesforce Developer Documentation.