> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cello.so/llms.txt
> Use this file to discover all available pages before exploring further.

# Salesforce Apex Triggers + Cello API

> This documentation guides you through the process of setting up Apex triggers in Salesforce to handle specific conditions related to Opportunities

This documentation guides you through the process of setting up Apex triggers in Salesforce to handle specific conditions related to Opportunities. The exemplary trigger will be designed to execute when an Opportunity meets two criteria:

1. The opportunity moved to the stage `Proposal/Price Quote`
2. It has a value in the custom field `cello_ucc`

<Info>
  Adjust according to your individual Salesforce setup and referral requirements

  Customize the code as needed based on your individual Salesforce setup. Cello requires at least a "demo done" event as well as a the revenue transactions of won opportunities.
</Info>

### Apex Trigger Configuration

### 1. Create an Apex Trigger

Find detailed documentation on [Apex triggers in the Salesforce docs](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm).

Navigate to Setup in Salesforce and follow these steps,:

* In the Quick Find box, type "Triggers" and select "Apex Triggers."
* Click "New Trigger" to create a new trigger.

```apex theme={null}
trigger OpportunityTrigger on Opportunity (after update) {
    OpportunityHandler.handleOpportunityUpdate(Trigger.new);
}
```

### 2. Create an Apex Class

Now, create an Apex class to handle the logic and API call. Implement the call to the [auto\$](/docs/cello-api) in the placeholder method `makeApiCall` in the below code.

This configuration ensures that the Cello API is triggered when an Opportunity meets the specified criteria. Customize the code as needed based on your individual Salesforce setup, API requirements and endpoint.

```apex theme={null}
public class OpportunityHandler {
    public static void handleOpportunityUpdate(List<Opportunity> updatedOpportunities) {
        for (Opportunity opp : updatedOpportunities) {
            // Check criteria: Opportunity in Proposal/Price Quote stage and cello_ucc value
            if (opp.StageName == 'Proposal/Price Quote' && opp.Cello_UCC__c != null) {
                // Make the API call
                makeApiCall(opp.Id, opp.Cello_UCC__c);
            }
        }
    }

    private static void makeApiCall(Id opportunityId, String celloUccValue) {
        // Your code to make the API call using HTTP methods, e.g., HttpRequest and HttpResponse
        // Example:
        // HttpRequest req = new HttpRequest();
        // req.setEndpoint('your API endpoint');
        // req.setMethod('POST');
        // req.setHeader('Content-Type', 'application/json');
        // req.setBody('{"opportunityId": "' + opportunityId + '", "celloUccValue": "' + celloUccValue + '"}');
        // HttpResponse res = new Http().send(req);
    }
}
```

### 3. Test and Deploy

* Test your trigger and class in the sandbox environment to ensure proper functionality.
* Reach out to the Cello team to confirm that provided event data is correct.
* Deploy the trigger and class to your production environment once testing is successful.

<Info>
  Important Notes

  * **Bulk Processing**: Ensure that your Apex trigger can handle bulk updates, as Salesforce triggers operate in bulk.
  * **Error Handling**: Implement appropriate error handling in your Apex class to manage API call failures.
</Info>
