# Salesforce Apex Trigger Example

Below is an exampole of a trigger handler for a Salesforce Apex Trigger. For a compleate udnerstanding of triggers in Salesforce please consult the salesforce documentation.

{% hint style="warning" %}
Remember to register a Remote Site Settings in salesforce for your Automation webhook.
{% endhint %}

```javascript
// Salesforce Apex Class to trigger automation
 
public class Flow_AccountTriggerHandler {

    private static String FlowUrl = 'https://AutomationWebhookUrl'; // Automation Endpoint
    private static String FlowApiKey = '<AccessKey>'; //Automation Access Key
    
    @future (callout=true)
    public static void makeCalloutToFlow(id accountId, String accountAsJson) {
        
        HttpResponse response = makeHttpPostCalloutToWebhook(
            FlowUrl,
            FlowApiKey,
            accountAsJson);
        
        updateAccountWithAccountNumber(
            accountId, 
            response.getStatusCode(),
            response.getBody());        
    }
    
    private static HttpResponse makeHttpPostCalloutToWebhook(
        String webhookEndpoint, 
        String xApiKeyHeaderValue, 
        String jsonBody)
    {
        HttpRequest request = new HttpRequest();
        request.setMethod('POST'); // POST Method
        request.setEndpoint(webhookEndpoint);
        // Begin Headers
        request.setHeader('x-api-key', xApiKeyHeaderValue); // Access Key
        request.setHeader('Content-Type', 'application/json'); // Body Format 
        request.setHeader('Accept','*/*'); //Required Can be */*, application/json, application/xml
        // Beging Body
        request.setBody(jsonBody);
        request.setTimeout(60 * 1000);
        return new Http().send(request);
    }

    private static void updateAccountWithAccountNumber(id accId, Integer statusCode, String jsonStr)
    {
        Account account = getAccountById(accId);        
        if (account != null) {
            if (statusCode == 200)
            {
                Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(jsonStr);
                String hasFailed = (String) results.get('HasFailed');
                if (hasFailed != null && hasFailed.toLowerCase().equals('false'))
                    account.AccountNumber = (String) results.get('CustomerId');
            }
            account.Description = 'Flow : ' + statusCode + ' ' + jsonStr;
            update account;                    
        }
    }
    
    private static Account getAccountById(id accountId) {
        List <Account> accounts = [SELECT Id,Name,Description FROM Account WHERE Id = :accountId];        
        if (!accounts.isEmpty()) return accounts[0];
        else return null;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.novacura.com/flow-connect/working-with-connect/create-and-design/automations/executing-automations-externally/salesforce-apex-trigger-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
