Enabling Automatic Cancellation With Flow
← Setting Up Invoice Correction
Your organization's business may require to cancel invoices automatically when certain conditions become true.
To this end, JustOn Billing & Invoice Management provides the specific Apex class InvoiceStatusChange. You can use this class to set up an according flow.
The Apex class covers multiple use cases. To cancel invoices, you can pass the following arguments to the Apex class:
Apex Variable Value | Description |
---|---|
Cancel |
Creates a draft cancellation invoice and sets the status of the original invoices to Canceled . |
Cancel + Finalize |
Creates a cancellation invoice that refers to the original invoice, finalizes the cancellation invoice and sets the original invoice to Canceled . |
Modified InvoiceStatusChange behavior with JustOn 2.80
JustOn 2.80 has modified the behavior of the involved Apex class InvoiceStatusChange.
Up to version 2.79, the class – and thus, the flow – synchronously executed the cancellation and finalization. This meant that after the flow execution was finished, the finalization and cancellation were also completed. However, the finalization did not include all batch classes of the FinalizeInvoiceChain but only the BatchFinalizeInvoice class. Therefore, the flow did not execute operations like settlement, balance assignment, installment creation, PDF generation, etc.
Starting with version 2.80, the flow calls the FINALIZEINVOICE batch chain to finalize the invoices. Consequently, it executes all operations of the included Apex classes (see FinalizeInvoiceChain). The batch chain runs asynchronously – this means that after the flow execution has finished, the invoice finalization is not yet completed.
The flow still executes the cancellation synchronously, though, before starting the finalization.
Note
Depending on your use cases, the flow setup and the objects to involve will vary.
- Click to enter Setup, then navigate to Process Automation > Flows.
- Click New Flow.
- Select
Record-Triggered Flow
and click Create. -
Configure the Start element.
Option Description Object The object whose record modifications are to change the invoice status Trigger The type of record change that triggers the flow Conditions The use case-specific trigger conditions, one or more filter criteria for evaluating certain field values Optimize for Actions and Related Records
-
Click and add the following Action element.
Option Description Action The Apex class to call, must be Change invoice status
Input Values Invocable variables for the Apex class
"Cancel", "Finalize" or "Cancel + Finalize": string value that specifies the operation to be executed, can beCancel
orCancel + Finalize
Invoice Id: specifies the invoices to be modified, usually produced via lookup relation from the original object to the related invoice, like{!$Record.ON_Invoice__r.Id}
Cancellation Reason: optional string value that specifies a cancellation reasonSpecify a label and an API name as required.
-
Click Save.
Specify a label and an API name as required.
-
Click Activate.
When the defined conditions are met, JustOn triggers the intended operation(s).
For help about creating flows, see Flows in the Salesforce Help.
Example use case: Cancel invoice when losing opportunity
Assume the following example: You create (draft) invoices from opportunities when they are Closed Won
. Now you lose an opportunity, and you must cancel the corresponding invoice. You want this to happen automatically when you set the opportunity stage to Closed Lost
.
Following this example, you need a lookup field on the Opportunity object that refers to the generated invoice.
Field | Data Type | Description |
---|---|---|
Invoice | Lookup (Invoice) | Shows the invoice that has been created for this opportunity |
Then you set up the flow as follows:
Flow Element | Option | Value |
---|---|---|
Start | Object | Opportunity |
Trigger | A record is updated |
|
Conditions | All Conditions Are Met (AND) 1: StageName Equals Closed Lost 2: StageName Is changed {!$GlobalConstant.True} |
|
Action | Action | Change invoice status |
Input Values | "Cancel", "Finalize" or "Cancel + Finalize": Cancel + Finalize Invoice Id: {!$Record.ON_Invoice__r.Id} Cancellation Reason: Invoice incorrect |
This triggers JustOn to execute the flow when the opportunity is set Closed Lost
: It creates a Cancelation
invoice that refers to the original invoice, finalizes the cancellation invoice and sets the original invoice to Canceled
.