# Salesforce Integration

This guide explains how to send Salesforce CRM events as goal events to ABsmartly using [Salesforce Flow HTTP Callouts](https://help.salesforce.com/s/articleView?id=platform.flow_http_callout.htm\&language=en_US\&type=5). This allows you to track CRM activities — such as opportunity creation, case submission, or lead conversion — as metrics in your experiments.

## Prerequisites

1. **ABsmartly account** with access to the Web Console and a valid **SDK API Key**. API Keys can be created under `Settings > API Keys` in the ABsmartly Web Console.
2. **Salesforce** org with access to **Flow Builder** and the ability to create Named Credentials and External Services.
3. A **unit type** (e.g. `user_id` or `email`) that is shared between your ABsmartly experiments and your Salesforce records so that events can be attributed to the correct user.

## Step 1 — Create a Named Credential

Named Credentials store the ABsmartly endpoint and authentication details securely.

1. In Salesforce Setup, search for **Named Credentials**.

2. Click the dropdown on **New** and select **New Legacy**.

3. Fill in the details:

   <!-- -->

   * **Label**: `ABsmartly Collector`
   * **Name**: `ABsmartly_Collector`
   * **URL**: Your ABsmartly Collector endpoint. For example: `https://your-subdomain.absmartly.io/v1`
   * **Identity Type**: `Named Principal`
   * **Authentication Protocol**: `No Authentication` (authentication will be handled via custom headers in the Flow)

4. Click **Save**.

## Step 2 — Create a Flow with an HTTP Callout

1. In Salesforce Setup, go to **Flows** and click **New Flow**.

2. Select **Record-Triggered Flow** and click **Create**.

3. Configure the trigger:

   <!-- -->

   * **Object**: Choose the object to track (e.g. `Case` for support cases).
   * **Trigger the Flow When**: `A record is created`.
   * **Condition Requirements**: Configure any filters (optional).

4. Add an **Action** element and select **Create External Service** or **HTTP Callout**.

5. Configure the HTTP Callout:

   <!-- -->

   * **External Service**: Select the `ABsmartly_Collector` Named Credential.
   * **Method**: `PUT`
   * **URL Path**: `/context`

6. Define the request body. Use a **JSON template** with values from the triggering record:

```json
{
  "publishedAt": "{!TEXT($Flow.CurrentDateTime)}",
  "units": [
    {
      "type": "email",
      "uid": "{!$Record.Contact.Email}"
    }
  ],
  "goals": [
    {
      "name": "salesforce_case_created",
      "achievedAt": "{!TEXT($Flow.CurrentDateTime)}",
      "properties": {
        "case_id": "{!$Record.CaseNumber}",
        "subject": "{!$Record.Subject}",
        "priority": "{!$Record.Priority}",
        "origin": "{!$Record.Origin}"
      }
    }
  ]
}
```

7. Add the required headers in the callout configuration:

| Header                  | Value                                                                        |
| ----------------------- | ---------------------------------------------------------------------------- |
| `X-API-Key`             | Your ABsmartly SDK API Key                                                   |
| `X-Application`         | The application name matching your ABsmartly Web Console (e.g. `www`)        |
| `X-Application-Version` | `0`                                                                          |
| `X-Environment`         | The environment name matching your ABsmartly Web Console (e.g. `production`) |
| `X-Agent`               | `salesforce-flow`                                                            |
| `Content-Type`          | `application/json`                                                           |

8. Click **Save** and **Activate** the Flow.

> **info**
>
> The `X-Application` and `X-Environment` values must match existing entries in your ABsmartly Web Console under `Settings > Applications` and `Settings > Environments`.

> **tip**
>
> Adjust the `units` type and value to match the unit type you use in your ABsmartly experiments. For example, if your experiments use a `user_id` unit, you may need to reference a custom Salesforce field that stores the user ID.

## Step 3 — Create the Goal in ABsmartly

1. In the ABsmartly Web Console, go to **Settings > Goals**.
2. Create a new goal with a name matching the `name` in your Flow payload (e.g. `salesforce_case_created`).
3. You can now use this goal as a metric in any of your experiments.

## Tracking Different CRM Events

You can create multiple Flows to track different Salesforce events as separate goals:

* **Opportunity closed won** — Trigger on `Opportunity` where `StageName = Closed Won`, goal name: `salesforce_opportunity_won`.
* **Lead converted** — Trigger on `Lead` where `IsConverted = true`, goal name: `salesforce_lead_converted`.
* **Case closed** — Trigger on `Case` where `Status = Closed`, goal name: `salesforce_case_closed`.
* **New account created** — Trigger on `Account` creation, goal name: `salesforce_account_created`.

## Verifying the Integration

1. Create a test record in Salesforce (e.g. a new Case) associated with a user whose identifier is also being tracked in an ABsmartly experiment.
2. Check the ABsmartly Web Console under the experiment's goals to verify the event was received.
3. In Salesforce, check the Flow's **Debug Log** or **Paused and Failed Flow Interviews** to confirm the HTTP callout was successful.

> **caution**
>
> Test your Flow in a Salesforce Sandbox environment before activating it in Production to avoid unexpected API calls.

If you have any issues setting up the Salesforce integration, please contact us at <support@absmartly.com>.
