# Connect Salesforce

A service account connects your project to Salesforce as one integration user, so Salesforce's own permissions decide what the [integration's](/docs/integrations.md) tools can read and change.

## Before you start

You need a project. See [Create a project](/docs/projects/create-project.md).

## Set up Salesforce

A Salesforce admin sets this up once per org.

| Part                   | Setting                                                                                           |
| ---------------------- | ------------------------------------------------------------------------------------------------- |
| Integration user       | User licence **Salesforce Integration**, profile **Minimum Access - API Only Integrations**       |
| Permission set licence | **Salesforce API Integration**, assigned to that user                                             |
| Permission set         | One, assigned to that user, with only the access below                                            |
| App                    | An External Client App with the client credentials flow, scope `api`, run as the integration user |

| Object  | Access     | Fields                                                                           |
| ------- | ---------- | -------------------------------------------------------------------------------- |
| Account | Read       | Name, Type, Industry, Phone, Website                                             |
| Contact | Read       | Name, Email, Phone, Title, Account                                               |
| Case    | Read, Edit | Read Subject, Description, Priority, Origin, Account, Contact; edit Reason, Type |

Record sharing still applies: unless org-wide defaults allow it (Public Read Only for Account and Contact, Public Read/Write for Case), share the records with the user.

Case Edit always allows Status and Owner. To block them, add this validation rule on Case, with your integration user's username in place of `veryfront@northwind.com`:

```text
$User.Username = "veryfront@northwind.com" && (ISCHANGED(Status) || ISCHANGED(OwnerId))
```

## Connect the service account

**API**

1. Set `PROJECT` to your project's slug, from its address in Studio (`/projects/<slug>`): `export PROJECT=<slug>`.
2. Get the project with your [API key](/docs/authentication/api-keys.md):

```bash
curl https://api.veryfront.com/projects/$PROJECT \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

3. Set `ENVIRONMENT_ID` to the `id` of the entry named `production` in its `environments`: `export ENVIRONMENT_ID=<id>`.
4. Add the app's consumer key:

```bash
curl -X PUT https://api.veryfront.com/projects/$PROJECT/environments/$ENVIRONMENT_ID/variables/SALESFORCE_SERVICE_ACCOUNT_CLIENT_ID \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value": "<consumer key>"}'
```

5. Add its consumer secret:

```bash
curl -X PUT https://api.veryfront.com/projects/$PROJECT/environments/$ENVIRONMENT_ID/variables/SALESFORCE_SERVICE_ACCOUNT_CLIENT_SECRET \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value": "<consumer secret>"}'
```

6. Add your org's My Domain address:

```bash
curl -X PUT https://api.veryfront.com/projects/$PROJECT/environments/$ENVIRONMENT_ID/variables/SALESFORCE_SERVICE_ACCOUNT_LOGIN_URL \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value": "https://northwind.my.salesforce.com"}'
```

7. List your cases. You should now see them in `content`, each with its `CaseNumber`, `Subject` and `Status`:

```bash
curl -X POST https://api.veryfront.com/integration-tools/salesforce__list_cases/run \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "x-veryfront-project-slug: $PROJECT" \
  -H "Content-Type: application/json" \
  -d '{"arguments": {}}'
```

### Troubleshooting

**If you see `provider-not-connected`**, "Authentication required for Salesforce.", the three variables aren't on Production. Add them, then list the cases again.

## With MCP

`my-project` is your project's slug.

1. Call `veryfront__list_environments` with `{ "project_reference": "my-project" }`, and take the `id` of `production`.
2. Call `veryfront__write_environment_variable` with `{ "project_reference": "my-project", "environment_id": "<the id>", "key": "SALESFORCE_SERVICE_ACCOUNT_CLIENT_ID", "value": "<consumer key>" }`.
3. Call it again with the key `SALESFORCE_SERVICE_ACCOUNT_CLIENT_SECRET` and the consumer secret.
4. Call it again with the key `SALESFORCE_SERVICE_ACCOUNT_LOGIN_URL` and the org's My Domain address.
5. Call `salesforce__list_cases` with `{}` on the project's endpoint, `https://api.veryfront.com/projects/my-project/mcp`, with the same `Authorization` header. It worked when it lists your cases, each with its `CaseNumber`.

## Next steps

* [Use integrations](/docs/mcp/integrations.md)
* [Set variables](/docs/runtime/environments.md)
