# Salesforce: Inbound Webhook Triggers (/docs/preference-management/inbound-webhook-triggers/salesforce)

This guide will run you through how to setup a custom webhook trigger in Salesforce to push consent changes into the Transcend Preference Store.

For other details about this integration, you can find documentation for that integration in the [Transcend Dashboard](https://app.transcend.io/infrastructure/integrations/new?filters=%7B%22text%22%3A%salesforce%22%7D\&integrationName=salesforce). These instructions can also be found by selecting your integration from the [Infrastructure -> Integrations](https://app.transcend.io/infrastructure/integrations) page and clicking on the "Connection" tab to view the instructions.

## Overview [#overview]

Setting up webhook triggers into Transcend involves 4 steps:

1. Write an Apex class to send a PUT request to the Transcend API.
2. Create an Apex Trigger to invoke the webhook when your consent fields changes.
3. Add your Sombra endpoint to the Salesforce Remote Site Settings
4. Test the integration.

## Step 1: Create an Apex Class for the Webhook [#step-1-create-an-apex-class-for-the-webhook]

- Log in to Salesforce and click the gear in the top right hand corner of the screen and click "Setup"

![Go to Salesforce settings](/images/2024-11-14-salesforce-settings.png)

- In the left hand side menu, search "Apex Classes" and select the option "Custom Code -> Apex Classes".

![Salesforce search Apex Classes](/images/screenshot/2024-11-14-salesforce-apex-classes.png)

- Click "New"

![Salesforce New Apex Class](/images/screenshot/2024-11-14-salesforce-new-apex-class.png)

- Paste the following code. You will need to switch out:

1. Your parition
2. Your API key
3. Your purpose/preference API shape

Once completed, hit save:

```java
public class TranscendWebhookHandler {
  @future(callout=true)
  public static void sendPreferences(String userId, Boolean salesOutreachEnabled) {
    String endpoint = 'https://multi-tenant.sombra.transcend.io/v1/preferences'; // Replace with your endpoint, see instructions below
    String partition = '776230e4-0fc7-4f62-9f35-44b71efdf8c3'; // Replace with your partition, see instructions below
    String apiKey = 'YOUR_API_KEY'; // Replace with your API key
    // created under [Developer Settings -> API Keys](https://app.transcend.io/infrastructure/api-keys)
    // with the scope `Modify User Stored Preferences`.

    // Construct the payload
    String payload = JSON.serialize(new Map<String, Object>{
      'records' => new List<Object>{
        new Map<String, Object>{
          'userId' => userId,
          'partition' => partition,
          'timestamp' => Datetime.now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\''),
          'purposes' => new List<Object>{
            new Map<String, Object>{
              'purpose' => 'SalesOutreach',
              'enabled' => salesOutreachEnabled
            }
          }
        }
      }
    });

    // Make the HTTP callout
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(endpoint);
    request.setMethod('PUT');
    request.setHeader('Content-Type', 'application/json');
    request.setHeader('Authorization', 'Bearer ' + apiKey);
    request.setBody(payload);

    try {
      HttpResponse response = http.send(request);
      if (response.getStatusCode() != 200) {
        System.debug('Error from API: ' + response.getBody());
      } else {
        System.debug('Webhook sent successfully: ' + response.getBody());
      }
    } catch (Exception e) {
      System.debug('Error during API call: ' + e.getMessage());
    }
  }
}
```

![Salesforce new Apex Class](/images/screenshot/2024-11-14-salesforce-apex-class-transcend-webhook-handler.png)

### Finding your Sombra URL [#finding-your-sombra-url]

- Go to [Infrastructure -> Sombra](https://app.transcend.io/infrastructure/sombra/sombras) in your Transcend account
- Choose the <GlossaryTerm id="sombra">Sombra</GlossaryTerm> gateway that makes sense for your use case (you most likely will want to use the Sombra marked as "Is Primary Sombra?")
- Copy the "Transcend Ingress URL" value as your Sombra base URL

![Preference Management Sombra Gateway](/images/screenshot/2024-11-11-copy-sombra-ingress.png)

### Finding your partition [#finding-your-partition]

The `partition` can be thought of as the ID of the consent database to update. In most implementations, you will just have one unique partition in your account. Multiple partitions can be useful in situations where you have multiple brands, partners or data controllers for which you need to separately track consent preferences for.

To grab this value:

- Go to [Preference Management -> User Preferences -> Settings](https://app.transcend.io/preference-store/settings)
- Ensure Preference Store toggle is enabled.
- Click on the button "Open Partition Settings"
- Locate your desired partition and click the copy button. The default partition is labeled `BUNDLE_ID` - this is most likely the value you will want to use.

![Preference Management Settings](/images/screenshot/2024-11-11-preference-management-settings.png)

![Preference Management Copy Partition Key](/images/screenshot/2024-11-11-partition-copy.png)

## Step 2: Create an Apex Trigger [#step-2-create-an-apex-trigger]

- Navigate to "Custom Code -> Apex Triggers"

![Salesforce settings for Apex Triggers](/images/screenshot/2024-11-14-salesforce-search-apex-triggers.png)

- Click "Developer Console"

![Open Apex Triggers Developer Console](/images/screenshot/2024-11-14-salesforce-apex-triggers.png)

- Navigate to "File -> New -> Apex Trigger"

![Create new Apex Trigger](/images/2024-11-14-salesforce-new-apex-trigger.png)

- Fill out the form for "New Apex Trigger"
  - **Name**: `ContactDoNotCallTrigger`
  - **sObject**: `Contact`
  - Click "Submit"

![Submit form to create Apex Trigger](/images/2024-11-14-salesforce-name-apex-trigger.png)

- Paste the following code. You can edit this code to update consent in Transcend based on a combination of field values.

```java
trigger ContactDoNotCallTrigger on Contact (after update) {
    for (Contact c : Trigger.new) {
        Contact oldContact = Trigger.oldMap.get(c.Id);

        // Check if any of these fields change
        // this logic may vary on how you plan to use each field
        // and how those map to preferences in Transcend
        if (
          c.DoNotCall != oldContact.DoNotCall ||
          c.Contact_Opt_Out__c != oldContact.Contact_Opt_Out__c ||
          c.HasOptedOutOfEmail != oldContact.HasOptedOutOfEmail
        ) {
            TranscendWebhookHandler.sendPreferences(c.Email,
            // opt user out in Transcend if any of these are disabled
            // this logic may vary on how you plan to use each field
             // and how those map to preferences in Transcend
             !c.DoNotCall &&
             !c.Contact_Opt_Out__c &&
             !c.HasOptedOutOfEmail
            );
        }
    }
}
```

![Paste Apex Trigger Code](/images/2024-11-14-salesforce-apex-trigger-code.png)

## Step 3: Add Remote Site Settings [#step-3-add-remote-site-settings]

- Go back to Setup settings and search "Remote Site Settings" and navigate to "Security -> Remote Site Settings"

![Search Remote Site Settings](/images/screenshot/2024-11-14-salesforce-setings-remote-sites.png)

- Click "New Remote Site" and fill out form and hit "Save"
  - **Remote Site Name**: `TranscendAPI`
  - **Remote Site URL**: This value can be found in [section below.](#finding-your-sombra-url) If Transcend hosted, this value is `https://multi-tenant.sombra.us.transcend.io` for EU hosting and `https://multi-tenant.sombra.transcend.io` for US hosting.
  - **Description**: `Privacy vendor used to manage consent preferences.`
  - **Active**: `true`

![Add new Remote Site for Transcend API](/images/screenshot/2024-11-14-salesforce-setings-remote-sites-transcend-api.png)

## Step 4: Test the Integration [#step-4-test-the-integration]

- Go back to Salesforce Home
- Navigate to `Contacts`
- Change one of the fields that you included in your Apex Trigger code above
- Navigate to [Preference Management -> User Preferences](https://app.transcend.io/preference-store/user-preferences) in the Transcend Admin Dashboard

![Add new Remote Site for Transcend API](/images/screenshot/2024-11-14-transcend-preference-store-updated.png)
