Invoice generation from quotations

Prev Next

Linda learned it is possible to automate the invoices generation from a quotation in Frisbii.
She asks Vijay to explain how it works.

Vijay suggests to Linda to read the following Trailhead articles beforehand, in order to fully understand his explanations:

Afterwards, Vijay explains how to configure this automation.

Trigger conditions on the quotation object

The automatic invoices generation from the quotation object is based on 4 fields:

  • Automatic invoicing

  • Already invoices

  • Automatic invoicing condition

  • Automatic invoicing exception

Note: The formulas in this table are given as an indication: Please customize the formulas according to your business needs. If you don’t need to define exceptions or conditions you can simplify the process.

Automatic invoicing
This check box is used by the invoicing process. It is already part of the package.


Already invoiced
This check box is set on true at the creation of the invoice in order to avoid invoice duplicates (invoices generated several times on the same quotation).


Automatic invoicing condition
This formula field identifies the quotations that need to be treated by the process.
If the box is checked, an invoice is generated from the quotation.
In our example, we would like to generate the invoice if:  

  • The quotation is not an exception (please see below).

  • The quotation is won.

(It might be necessary to put Gagnée instead of Won. Please try that out, if Won is not working for you.)

The formula is based on these two fields:

  • Automatic invoicing exception

  • Stage (sofactoapp__Etape__c)// packaged field

Please adapt the conditions to your business needs.        

AND(Automatic_invoicing_exception__c = FALSE,
ISPICKVAL(sofactoapp__Etape__c , "Won"))


Automatic invoicing exception
This formula field groups the exceptions, in which cases the generation of invoices should not happen.
If the box is checked, the quotation is not taken into account by the process.  
In our example, the quotation is marked as an exception if:  

  • the amount before tax < 0

  • the quotation was already billed manually

  • the quotation was already billed by the process

This formula is based on these fields:

  • Packaged fields:

    • Amount excl. VAT (sofactoapp__Montant_HT_apres_remise__c)

    • Invoiced Amount (sofactoapp__Montant_facture__c)

  • Already invoiced (Already_invoiced__c //created during this procedure)

Please adapt the condition to your business needs.
       

OR( sofactoapp__Montant_HT_apres_remise__c <= 0,
sofactoapp__Montant_facture__c != 0,

(*If you just came back to copy the formulas, please click here to go back to the steps.)


Now that Linda understood the purpose of these fields, she can start the configuration of the process.

Setting up the needed fields for the automation

We recommend you to read these trailhead articles, if you did not have the occasion to train yourself on these topics:

The checkbox Automatic invoicing is part of the package and is already present in the object.
So Linda configures only the check box and the two formula fields.
Since Automatic invoicing refers to Automatic invoicing exception, and this one to Already invoiced, she has to set up the fields in this order:

  • Check box Already invoiced

  • Formula field Automatic invoicing exception

  • Formula field Automatic invoicing condition

Lets start by setting up the field Already invoiced:

  • Check box

  • Object : Quotation

Here you can see Linda's result:

Here you can find the procedure to set up the two formula fields:


Steps

1. Go to Setup > Object Manager > Quotation
2. Click on Fields & Relationships
3. Click on New
4. In Step 1: Choose the field type Formula
5. In Step 2: Choose the output type Checkbox and insert the field label Automatic invoicing exception
6. In Step 3: Switch to the tab Advanced Formula and compose the formula that fits to your business needs.

Note: If you copy these formulas from the top of the article, please paste it into a text editor in order to remove the formatting of the text. Sometimes the Formula editor does not accept copy pasted formulas due to the formatting.

7. Verify the formula syntax.
8. Insert a telling description and a help text that explains the field to the users.
9. In Step 4: Establish field-level security by checking the boxes of the profils you want to give the right to modify the field, according to your company security policies.
10. In Step 5: Instead of checking the box here, we recommend to go to each page layout of the quotation object you want to put the fields on, in order to put them manually in the right section.

Please repeat this procedure for the field Automatic invoicing condition.

Note: In order to easily debug the process, if a quotation is not processed because it is an exception, make life easier for users - and reduce your work - by putting conditions / exceptions in a non-technical language in the help text of these fields.

When the user understands why the quotation is an exception, he or she can modify the quotation to meet the conditions or at least understands why the quotation has not been processed.

Here you can see Linda's results:

After creating these fields, Vijay explains to Linda how to use them in the process builder.

Note: Don’t forget to add the fields on your page layout so that users can check them, if there is a problem.


Configuration of the control field

In order to inform the invocable method MarkOfferForAutoinvoicing under which conditions it should check the Automatic invoicing box (which will trigger the next process), you need to configure the control field in the Administration tab.

Steps

1. Go to the Administration tab of the Frisbii application.
2. Select the Automatic invoicing condition field that you created above as Control field for automatic creation of invoices from quotations.
3. Validate your choice.


As explained above, we use the invocable method MarkOfferForAutoinvoicing to automatically create invoices from the offers. It will then check the Auto invoicing box according to the conditions we defined beforehand.
Now we will set up an automation that applies the Class Apex Invoice_Offers, in order to generate invoices.

Creation of the flow

Vijay explains to Linda that he now uses only flows to automate processes in Salesforce, and no longer through the process builder. Together, they set up two flows :

  • The first one on the offer object to create the invoice.

  • The second one on the object of the invoice which allows to update the offer after the creation of the invoice to indicate that the offer has already been invoiced.

Let's start with the flow on the supply side.

Steps

  1. Go to Set Up > Process Automation > Flow or type Flow in the quick search bar.

  2. Choose Triggered Flow by a recording.

  3. Double click on the symbol Start to set up the start.

3.1 Select the Offer object.
3.2. Trigger the flow when: A record is updated.
3.3. Define as input conditions :

  • All conditions are met (AND)

    • sofactoapp_Auto_invoicing__c equal to True

    • Deja_invoice__c equal to False

3.4. Run flow for updated records Only when a record is updated to meet the requirements of the conditions.
3.5. Optimize flows for : Actions and associated records.
3.6. Click on Finish.

     4. Click on the More to add an item.

4.1. Click on Action and then search for Apex in the search bar.
4.2. Choose Invoice_Offers.
4.3.Give the action a name in the Label field and make sure the API name is written correctly.
4.4. Set the input values by putting {!$Record.Id} in OfferId field.
5. Save your flow.
6. Activate it.


That's a good thing done! Before moving on to testing, Linda and Vijay set up the second feed to update the offer.

Steps

1. Go to Setup > Process Automation > Flows or type Flows in the quick search bar..
2. Choose Flow triggered by a recording.
3. Double click on the Start symbol to Configure Startup.

3.1. Select the Invoice object.
3.2. Trigger the flow when: A record is created.
3.3. Define as input conditions :

  • Condition requirements: Formula Evaluates to True

AND
(NOT(ISBLANK({!$Record.sofactoapp__Offre__c})),
{!$Record.sofactoapp__Offre__r.sofactoapp__Auto_facturation__c} = true,
{!$Record.sofactoapp__Offre__r.Deja_facturee__c} = false)

We will verify :

  • if an offer is linked to the invoice,

  • if the automation has been executed on this offer (the Auto billing box is checked)

  • Already invoiced is unchecked on the offer

3.4. Check the syntax by pressing the button.
3.5. Optimisez les flux pour : Actions et enregistrements associés. Optimize the flows for : Actions and associated records.
3.6. Click on Finish.

4. Click the Plus to add an item.

4.1. Click Update Trigger Record.
4.2. Give a meaningful name.
4.3. How to search for records to update and set their value: Check Specify conditions to identify records, then set fields individually.
4.4. Update records of this object type: Offer
4.5. Filter the Offer records:

  • All conditions are met:

  • ID equal to {!$Record.sofactoapp__Offer__c}

4.6. Define the field values for Offer Records:

  • Already_invoiced__c* <-- True

*Please note: Be sure to check how you named this field to put the correct API name on it.

4.7. Click on Done.
5. Save your flow.
6. Activate it.

That was fast! Let's go straight to planning the Apex class.

Schedule the Apex job

If you want the invoice generation process to start at a certain time, you have to use a scheduled Apex job. Here is the procedure of Salesforce for Scheduling Apex jobs.

Steps

1. From Setup, enter Apex Classes in the Quick Find box, select Apex Classes, then click Schedule Apex.

2. Select the class: MarkOfferForAutoInvoicing

3. Specify how often the Apex class is to run.
4. Specify the start and end dates for the Apex scheduled class. If you specify a single day, the job only runs once.
5. Specify a preferred start time. The exact time the job starts depends on service availability.
6. Click Save.

Note:
(1) If you plan the Apex job during the day, remember that Frisbii users can still work on quotations that meet the criteria. If the process needs to access a quotation which is in use, you will receive an error message and the process will stop.

(2) You can find the scheduled job afterwards under Setup > Environments > Jobs > Scheduled Jobs. If you want to modify the job, you have to delete it and schedule the Apex class again.

Testing approach

Congratulations ! You configured the automation completely. Now let's not forget to test our setup.

Preparation

First, make sure you test on a quotation that meets the criteria, and therefore should be automatically invoiced.

For example in the case of Cloud Kicks:

  • the Quotation Stage is won

  • the Automatic invoicing exception box is not checked (false)

  • the field Account is filled out (mandatory for creating an invoice!)

and you have added the four fields on your page layout:

  • Automatic invoicing

  • Already invoiced

  • Automatic invoicing condition

  • Automatic invoicing exception

Steps

  1. Open the Developer Console

  2. In the Debug menu > Open Execute Anonymous Window

  3. In the pop up window Enter Apex Code paste that expression:

sofactoapp.MarkOfferForAutoInvoicing  
autoOffer = new sofactoapp.MarkOfferForAutoInvoicing(); 
autoOffer.execute(null);

  1. Click on Execute to run the script.

Was the test successful?

The test was successful, if:

  • an invoice is linked to your quotation

  • the check box Automatic invoicing exception is checked

  • he check box Automatic invoicing is not checked anymore

  • the check box Already invoiced is checked

This is not the case?

Please verify:

  • The formulas of the fields:

    • Automatic invoicing condition

    • Automatic invoicing exception

  • On your test quotation:

    • Is the Automatic invoicing condition checked?

      • No? > Compare your quotation with your set conditions

    • Is the checkbox Automatic invoicing exception checked?

      • Yes? > Modify your test quotation in order not to meet the exception criteria.

  • The configuration of your process:

    • Is the process activated? 

    • If yes, please read again the procedure and verify your process. 

  • You tested twice on the same quotation and it is not working, even if you deleted the invoice? 

    • Modify an option that makes the Automatic invoice condition check box unchecked, save, and then put the quotation back in condition

    • Note: The process builder needs a modification of this kind, in order to be able to reprocess the record