action.skip

Setting Up Automatic Draft Invoice Deletion

← Configuring Draft Editing Options

Certain use cases may require to delete draft invoices based on given criteria.

As an example, assume you use the continuous invoice run to produce invoices as early as possible and to update them when required – there may be changes to the account (like a new address or bank account), to the subscription (a new item) or to the source object. These modifications may invalidate the information on already created draft invoices, which therefore must be deleted. The next continuous invoice run then regenerates the previously removed draft invoice with the correct information.

To this end, JustOn Billing & Invoice Management provides the specific Apex class DraftInvoiceRemover, which you can use to set up a flow to delete invalidated draft invoices.

Note

Be aware that the draft invoice remover not only deletes invoices associated with an invoice run, but all invoices with the status Draft that are not excluded from deletion.

draft_invoice_remover_use_cases
Modifications on a parent or a source record trigger the draft invoice removal

Flow details
  • JustOn executes the draft invoice remover when the criteria of the flow are met.
  • The remover determines all draft invoices that are related to the records in the scope of the flow.
  • The remover queues the delete job for the draft invoices.
  • The delete job is executed until all relevant invoices are deleted.
  • The number of jobs equals the number of invoices divided by the batch scope.
  • You can monitor the jobs on the Apex Jobs page in Setup.
  • The job sends an email that lists all invoices that could not be deleted. You must delete these invoices manually.

Setting up the automatic draft invoice deletion involves the following tasks:

Setting Up Draft Invoice Remover Flow

Note

Depending on your use cases, the flow setup and the objects to involve will vary.

  1. Click to enter Setup, then navigate to Process Automation > Flows.
  2. Click New Flow.
  3. Select Record-Triggered Flow and click Create.
  4. Configure the Start element.

    Option Description
    Object The object whose record modifications are to trigger the draft invoice deletion
    For use case A, the invoice parent (account/subscription), related like Invoice → invoice parent via lookup or formula with CASESAFEID()
    For use case B, the invoice source object, related like invoice source → Invoice via lookup or formula with CASESAFEID()
    For objects marked as single source object, that is, where ON_IsItem = true, the use case B applies.
    Trigger The type of record change that triggers the flow, must be A record is created or updated
    Conditions The use case-specific trigger conditions, one or more filter criteria for evaluating certain field values
    Optimize for Actions and Related Records
  5. Click and add the following Action element.

    Option Description
    Action The Apex class to call, must be Remove draft invoices
    Input Values Invocable variables for the Apex class
    Is Parent: is to be set True for invoice parents (use case A), False for invoice sources (use case B)
    Lookup Field Name: string value that specifies the API name of the relevant ID field, for the invoice parent (use case A) located on the invoice (for example, Account__c), for the invoice source (use case B) located on the source object (usually ON_Invoice__c)
    Object Id: the ID field of the related object, for the invoice parent (use case A) the ID of the parent object, for the invoice source (use case B) the ID of the source object

    Specify a label and an API name as required.

  6. Click Save.

    Specify a label and an API name as required.

  7. 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.

Flow configuration examples

draft_invoice_remover_use_cases

Following the two use cases, the corresponding flow configurations may result like this:

Flow Element Option Example Values Use Case A
Identify and delete draft invoices for an account whose bank account information has been modified
Example Values Use Case B
Identify and delete draft invoices from an order whose effective date has been modified
Start Object Account Order
Trigger A record is created or updated A record is created or updated
Conditions ON_BankAccount__c Is changed {!$GlobalConstant.True} EffectiveDate Is changed {!$GlobalConstant.True}
Action Action Remove draft invoices Remove draft invoices
Input Values Is Parent: {!$GlobalConstant.True}
Lookup Field Name: Account__c
Object Id: {!$Record.Id}
Is Parent: {!$GlobalConstant.False}
Lookup Field Name: ON_Invoice__c
Object Id: {!$Record.Id}

Configuring Batch Scope

The invoices are deleted by a queueable asynchronous batch process in order to keep the governor limits under control. This also enables bulk operations on parent and child objects with larger batch sizes than a usual invoice deletion would allow.

According to your individual requirements, you may have to adjust the batch process scope. The scope specifies the execution size, that is, the number of records to be processed by the batch class in one go.

To this end, you create a dedicated Batch Settings record.

  1. Click to enter Setup, then open Custom Settings.

    In Salesforce Lightning, navigate to Custom Code > Custom Settings.

    In Salesforce Classic, navigate to Develop > Custom Settings.

  2. Click Manage in the row of Batch Settings.

  3. Click New.
  4. Specify the details as necessary.

    • Name: DraftInvoiceRemover
    • Scope: 50
  5. Click Save.

Info

Be aware of the following specifics:

  • The scope depends on the number of objects and relationships. Test the setting to determine the appropriate value. A larger scope may work, but can can produce limit exceptions.
  • Limit exceptions are reported by email. If necessary, reduce the scope accordingly.

Excluding Draft Invoices From Deletion

Your business may require to exclude the draft invoices produced produced by a particular invoice run from the deletion by the draft invoice remover flow.

To this end, you mark the corresponding invoice run as final.

  1. Open the Invoice Runs tab.
  2. Click the number of the invoice run whose draft invoices you want to exclude from deletion.
  3. Click next to the Is Final checkbox and select it.

    Alternatively, you can click Edit in the invoice run view and then select the Is Final checkbox.

  4. Click Save.

    This excludes the draft invoices produced by the current invoice run from deletion by the draft invoice remover flow.