In order to inform the invocable method MarkOpportunityForAutoInvoicing 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 opportunitires.**
3. Validate your choice

## 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 opportunity object to create the invoice.
- The second one on the invoice object which allows to update the opportunity after the creation of the invoice to indicate that the opportunity has already been invoiced
Let's start with the flow on opportunity.
### 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 **Opportunity** 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**
- Already_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 **Opportunity_2_Invoice**. 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**](http://record.id/)**}** in **Opportunityld** field.
5. Save your flow. That's a good thing done! Before moving on to testing, Linda and Vijay set up the second flow to update the opportunity.
### 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**
```plaintext
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.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: Opportunity 4.5. Filter the Opportunity records:
- All conditions are met:
- ID equal to {!$Record.sofactoapp__Opportunit__c}
4.6. Define the field values for Offer Records:
- Alreadyinvoiced__c* <-- **True**
***Please note:** Be sure to check [how you named this field](/frisbii-app/docs/en/automatisation-renommer-des-pdfs-de-facture-doffre) 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 go through a scheduled Apex Class, here is the procedure of Salesforce for [Scheduling Apex jobs](https://help.salesforce.com/articleView?id=code_schedule_batch_apex.htm&type=5).
### Steps
1. From Setup, enter *Apex Classes* in the Quick Find box, select **Apex Classes,** then click **Schedule Apex**.
2. Select the class: *MarkOpportunityForAutoInvoicing*
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 task during the day, remember that Frisbii users can still work on the opportunities that already fall under the conditions. If the process wants to access an opportunity, 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 > EnviLa tâche planifiée est ensuite visible dans Configuration > 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 an opportunity that meets the criteria, and therefore should be invoiced automatically.
For example in the case of Cloud Kicks:
- the **Opportunity Stage** is won
- the **Automatic invoicing exception** box is not checked (false)
and you have added the four fields in 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:
```plaintext
// String CRON_EXP = ‘ss mm hh dd MM ? AAAA’;
```
```plaintext
String CRON_EXP = '
00 00 10 02 03 ? 2020';
String lv_jobId = System.schedule(
'MarkOpportunityForAutoInvoicing_Test'+Datetime.now(),
CRON_EXP,
new sofactoapp.MarkOpportunityForAutoInvoicing()
);
```
4. Put the date and hour in order to schedule the process in one minute.

1. Click on **Execute** to run the script.
## Was the test successful?
- Automatic invoicing
- Already invoices
- Automatic invoicing condition
- Automatic invoicing exception
The test was successful, if:
- an invoice is linked to your opportunity
- the check box **Automatic invoicing** is checked
- the check box **Already invoiced** is checkedThis is not the case? Please verify:
- The formulas of the fields:
- Automatic invoicing condition
- Automatic invoicing exception
- Is on your test opportunity:
- The **Automatic invoicing condition** checked?
- No? > Compare your opportunity with your set conditions
- The checkbox **Automatic invoicing exception** is checked?
- Yes? > Modify your test opportunity 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 opportunity 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 opportunity back in condition
- Note: The process builder needs a modification of this kind, in order to be able to reprocess the record