## Sync Frontegg accounts and users to Salesforce

The Salesforce integration keeps your CRM up to date with what happens in Frontegg. When an account (tenant) or a user is created, updated, or deleted in Frontegg, Frontegg writes the change to Salesforce as an **Account** or a **Contact**. Your sales and marketing teams can then work with current product data without a manual export or a custom webhook receiver.

### What gets synced

Frontegg syncs the following events. You choose which of them are sent when you set up the connection.

| Frontegg event | Event key | What happens in Salesforce |
|  --- | --- | --- |
| Account created | `frontegg.tenant.created` | Creates or updates the Account. |
| Account updated | `frontegg.tenant.updated` | Creates or updates the Account. |
| Account deleted | `frontegg.tenant.deleted` | Updates the Account and sets **Frontegg status** to `Deleted`. The record is kept. |
| User created | `frontegg.user.created`, `frontegg.user.signedUp` | Creates or updates the Contact and links it to the user's Account. |
| User activated | `frontegg.user.activated` | Creates or updates the Contact. |
| User updated | `frontegg.user.updated` | Creates or updates the Contact. |
| User removed from an account | `frontegg.user.removedFromTenant` | Clears the Contact's Account when the user is removed from the account the Contact is linked to. |
| User deleted | `frontegg.user.deleted` | Updates the Contact and sets **Frontegg status** to `Deleted`. The record is kept. |


How the sync behaves:

- **Records are matched by Frontegg ID.** Frontegg uses the `Frontegg_ID__c` external ID field to find the record in Salesforce. If a record with that ID exists, it is updated. Otherwise, it is created. Sending the same event twice does not create duplicates.
- **Deleted means marked, not removed.** Frontegg never deletes records in Salesforce. Deleted accounts and users keep their record and get the status `Deleted`, so your CRM history is preserved.
- **Only new changes are synced.** After you connect, Frontegg sends changes as they happen. Existing accounts and users are not imported.
- **Missing accounts are created.** If a user event arrives before its Account exists in Salesforce, Frontegg first creates a placeholder Account named `Frontegg account <account ID>` with the status `Active`, and then writes the Contact. A later account event fills in the real account details.
- **Users in several accounts.** A Contact is linked to the user's active account in Frontegg.


### Before you begin

Prerequisites
- A Salesforce org where you can create an External Client App and custom fields. You can use a production org or a sandbox.
- A Salesforce admin, who completes the Salesforce setup in [Step 1](#step-1-prepare-salesforce).
- A Frontegg portal user who can manage webhooks and integrations in the environment you want to connect.
- One connection per environment. Frontegg allows a single Salesforce connection in each environment.


### Step 1: Prepare Salesforce

Ask your Salesforce admin to complete these steps once. The Frontegg portal shows the same checklist when you start to connect Salesforce, so you can share it with your admin.

1. **Create an External Client App** with the client credentials flow.
  - In Salesforce Setup, create a new External Client App.
  - In the OAuth settings, enable OAuth and select the **Enable Client Credentials Flow** option.
  - Add the API scope to the selected OAuth scopes.
  - Open the app policies and set the **Run As** user for the client credentials flow to the integration user from the next step.
2. **Set up an integration user.** Choose or create a Salesforce user that can read, create, and edit **Account** and **Contact** records and can use the API. Frontegg acts as this user, so its permissions decide what Frontegg can write.
3. **Add a Frontegg ID field** to both the **Account** and the **Contact** object:
  - **API name:** `Frontegg_ID__c`
  - **Type:** Text
  - **External ID:** selected
  - **Unique:** do not allow duplicate values
4. **Add a Frontegg status field** to both the **Account** and the **Contact** object: a picklist with the API name `Frontegg_status__c` and the values `Active` and `Deleted`.
5. **Give the integration user access** to both custom fields (read and edit field-level security).
6. **Copy the credentials.** From the External Client App, copy the **Consumer Key** and **Consumer Secret**, and note your Salesforce domain, for example `https://mycompany.my.salesforce.com`.


#### Optional: keep the newest data when updates arrive out of order

Events can reach Salesforce out of order. Without extra setup, the last update that arrives wins. To make Salesforce reject an older update and keep the newer data, do the following on **both** the Account and the Contact object:

1. Add a field with the label `Frontegg updated at (ms)`, the API name `Frontegg_updated_at_ms__c`, and the type **Number** (length 18, 0 decimal places). Give the integration user read and edit access to it.
2. Add a validation rule with the error message `FRONTEGG_STALE_EVENT` and the following formula:


```text
AND(NOT(ISNEW()),
NOT(ISBLANK(PRIORVALUE(Frontegg_updated_at_ms__c))),
Frontegg_updated_at_ms__c <
PRIORVALUE(Frontegg_updated_at_ms__c))
```

1. In [Step 4](#step-4-review-the-field-mapping), map the Frontegg **Event time** field to `Frontegg_updated_at_ms__c` for both objects. If you add the field later, add the mapping row then.


When Salesforce rejects an update with this message, Frontegg treats the event as outdated and skips it.

### Step 2: Connect Salesforce in the Frontegg portal

1. Open the **Frontegg portal** and select your environment.
2. Go to **Integrations** and click the **Salesforce** card to start the **Connect Salesforce** setup.
3. On the **Prepare** step, confirm that your Salesforce admin completed the checklist from [Step 1](#step-1-prepare-salesforce).
4. On the **Connect** step, enter the credentials:
  - **Salesforce domain:** your org's HTTPS domain, for example `https://<name>.my.salesforce.com`. For a sandbox, use `https://<name>.sandbox.my.salesforce.com`.
  - **Consumer key:** the Consumer Key of the External Client App.
  - **Consumer secret:** the Consumer Secret of the External Client App.
5. Click **Test connection**. When the test succeeds, Frontegg shows the Salesforce user and org it connected to. You can continue only after a successful test.


### Step 3: Choose what to sync

On the **Trigger event** step, enter a name for the connection and select the events to send to Salesforce. The events are grouped into **Accounts** and **Users** and match the events listed in [What gets synced](#what-gets-synced). All events are selected by default.

### Step 4: Review the field mapping

The **Field mapping** step controls which Salesforce field receives each Frontegg field. Frontegg starts from a default mapping that works with the fields you created in [Step 1](#step-1-prepare-salesforce).

**Account**

| Frontegg field | Salesforce field | Notes |
|  --- | --- | --- |
| Name | `Name` | Required. |
| Account ID | `Frontegg_ID__c` | Required and locked. |
| Website | `Website` | Optional. |
| Status | `Frontegg_status__c` | Required. |


**Contact**

| Frontegg field | Salesforce field | Notes |
|  --- | --- | --- |
| Name | `FirstName` and `LastName` | Required. Maps to exactly two Salesforce fields. |
| Email | `Email` | Required. |
| Phone | `Phone` | Optional. |
| User ID | `Frontegg_ID__c` | Required and locked. |
| Status | `Frontegg_status__c` | Required. |
| Account | `AccountId` | Required and locked. Links the Contact to its Account. |


You can also map these optional Frontegg fields:

- **Event time**, for example to `Frontegg_updated_at_ms__c` as described in the optional stale-update setup. Available for Accounts and Contacts.
- **External ID** of the user. Available for Contacts.
- **Metadata**, to copy a single key from the user's metadata into a Salesforce field. Available for Contacts. Select **Metadata** and enter the metadata key.


Frontegg checks the mapping against your Salesforce org. Resolve any issues it shows before you continue.

If a user has no last name, `LastName` is set to `N/A` because Salesforce requires it.

Click **Finish**. Frontegg saves the connection and turns it on. Changes made in Frontegg from this point are synced to Salesforce.

### Manage the connection

Open **Integrations** and select the Salesforce connection. The connection page has three tabs:

- **Settings:** change the name, credentials, or events. New credentials must pass **Test connection** before you can save them. You can also delete the connection here.
- **Field mapping:** edit the mapping. Frontegg validates the changes against your Salesforce org before saving.
- **Logs:** review the delivery attempts for this connection.


Use the toggle at the top of the page to pause or resume the sync. Frontegg does not send events that occur while the connection is paused.

### Troubleshooting

#### The connection test fails

Check the following in Salesforce:

- The domain is your org's `.my.salesforce.com` domain and starts with `https://`.
- The Consumer Key and Consumer Secret belong to the External Client App.
- **Enable Client Credentials Flow** is selected, and a **Run As** user is set.
- The integration user is active and can use the API.


#### The mapping shows Not found in Salesforce

The mapped field does not exist in your org, or the integration user cannot create and update it. Check that the field exists on the correct object and that the integration user has field-level access.

#### The connection needs attention

Delivery failures follow the same rules as webhooks. See [Webhook retries and auto-disable](/ciam/guides/integrations/webhooks#webhook-retries-and-auto-disable).