---
title: "Configuring an Apex Trigger for Outbound Sync"
canonical: "https://help.fieldbuddy.com/space/FBDOCS/2078311179/Configuring%20an%20Apex%20Trigger%20for%20Outbound%20Sync"
format: markdown
---
For outbound data synchronisation an object requires a Salesforce Trigger (read more about triggers in the [Salesforce documentation](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm)). This small code will fire (*trigger)* FieldBuddy Connect to start an outbound message. In this chapter a trigger will be created using the given Example. The method for creating a trigger is depending on the Salesforce organisation. This chapter will assume that the organisation is not yet a production organisation. Only in trial- or sandbox-organisations a trigger can created using the following steps.

In case the organisation is production. Creation of the trigger requires to be done via Deploying it from Sandbox via Changeset, or [SFDX](https://developer.salesforce.com/tools/sfdxcli) or the [Workbench](https://www.google.nl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&cad=rja&uact=8&ved=0CEQQFjAEahUKEwibw5yi1djIAhUCgXIKHWWOAps&url=https%3A%2F%2Fdeveloper.salesforce.com%2Fdocs%2Fatlas.en-us.api_rest.meta%2Fapi_rest%2Fquickstart_using_workbench.htm&usg=AFQjCNF8DuxE7FJ-VRnZRO_f5ErpSZHx1A&bvm=bv.105841590,d.bGQ).

Any code requires test-coverage from Salesforce. This code will require test-code.

> ⚠️ Tools as the Developer Console, Eclipse and the Workbench are powerful and can impact your Salesforce organisation being used in the wrong way.

#### Example trigger

 This example is a trigger on FIELDBUDDY__Work_Order__c. The trigger is named: `tr_EOL_WO`

```java
trigger tr_EOL_WO on FIELDBUDDY__Work_Order__c (
    after delete, after insert, after undelete,
    after update, before delete,
    before insert, before update) {
    FBCONNECT.Triggers.syncAllEvents();                        
}
```

or for Account

```
trigger tr_EOL_Account on Account (
    after delete, after insert, 
    after undelete,
    after update, before delete,
    before insert, before update)
{
    FBCONNECT.Triggers.syncAllEvents();
}
```

You don't have to make this triggers for Inbound mappings and child objects.

#### Trigger with Recursive Control check **

> ⚠️ This trigger may lead into various scenarios – contact FlexConnect Support to confirm that this will work in your case. It also requires creation of a Apex Class – quite similar how trigger is created. Class in this case needs to be created first


Here is also an example of a trigger if you need connect to be fired only once during transaction. 

```
/* Class for duplicate management */
public class cls_EOLDuplicateExecutionManager{
    private static Boolean RunWOTrigger = true;
	public static Boolean checkRunWOTrigger() {
        Boolean run =  RunWOTrigger;
        if (RunWOTrigger == true) {
           RunWOTrigger = false;
        }
        return run;
    }
}
```


```
/* Trigger Example */
trigger tr_WO_EOL on Work_Order__c (after delete, after insert, after undelete,
                             after update, before delete,
                             before insert, before update) {
    if (cls_EOLDuplicateExecutionManager.checkRunWOTrigger()) {
        FBCONNECT.Triggers.syncAllEvents();
    }
}
```

#### Steps to create trigger via Developer Console

> ⚠️ Warning
> ⚠️ 
> ⚠️ Triggers must be deployed via a Sandbox or Eclipse in case Production Org is **Active**. In a test Production Org, just create the trigger as instructed in this page. Note that the (custom) object you create the trigger for, should be created first. For example, if there is no custom object, then create the Object first.


1. Open up the 'Developer console' by going to the top-right menu.
2. Click in the menu File > New > Apex Trigger
  
3. Name: `tr_EOL_<Name of the object>`, for example: `tr_EOL_Account`. And select any object, for example: Account.
  
  
4. Copy the Example trigger and replace the content in the editor.
5. Replace the first line using the name of the trigger and the object. Replacing will result in the following code:
  
6. Go to 'File' in the top menu and click 'Save'.

The trigger will now execute FieldBuddy Connect when a user performs the following actions:

- Insert (Create)
- Update (Edit)
- Delete
- Undelete

Test this by running the Outbound Data Sync manually.

#### Example test-coverage

This example is a class with test-coverage for the objects Account, Opportunity and Case. The class is named: EOL_trigger_test.

> ℹ️ Writing test-coverage can be customer specific. Read the Salesforce documentation in how to achieve this: [https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods](https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods)

```java
@isTest 
private class EOL_trigger_test {
  static testMethod void createRecords() {

    Account a = new Account(Name = 'testname');
    insert a;

    Opportunity op = new Opportunity(Name = 'testname', StageName = 'Won' , CloseDate = Date.newInstance(2030, 3, 16) );
    insert op;

    Case c = new Case(Subject = 'tstSubject', Status = 'Nieuw');
    insert c;

  }
}
```

#### Steps to create test-coverage

1. Open up the 'Developer console' by going to the top-right menu.
2. Name: EOL_trigger_test
3. Copy the Example test-coverage. It should look like:
4. Go to 'File' in the top menu and click 'Save'.
5. Go to SetUp> Develop> Apex Triggers and check if the Trigger you've created is listed in here.

How to run test-classes can be found in the Salesforce documentation: [https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests_running.htm](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests_running.htm) 

> ℹ️ Salesforce might require to recompile all classes and run test-classes from the standard UI in order to obtain the updated test-coverage.


Previously (in versions 1.13.x) another format of a trigger was used – with the code below instead of `FBCONNECT.Triggers.syncAllEvents();`

```
new FBCONNECT.Triggers()
            .bind(FBCONNECT.Triggers.Evt.afterinsert, new FBCONNECT.cls_FBAfterInsertHandler())
            .bind(FBCONNECT.Triggers.Evt.afterupdate, new FBCONNECT.cls_FBAfterUpdateHandler())
            .bind(FBCONNECT.Triggers.Evt.beforedelete, new FBCONNECT.cls_FBBeforeDeleteHandler())
            .manage();
            
        
```

Both will work, but it is advised to use `FBCONNECT.Triggers.syncAllEvents();`, since another one will be deprecated in the future