# Send conversions to Salesforce

Log every purchase, lead or sign-up from your other sources as a completed Task on the person's Lead or Contact, created by email when new.

Salesforce can be an event destination like an ad platform: each conversion a profile receives is logged as a completed Task on the person's Lead or Contact.

## Before you start

- Connect Salesforce to the profile ([Connect](/platforms/salesforce/connect)). The connection's `api` scope covers this, so no reconnect is needed.
- The Salesforce user who connected needs Create on Leads (or Contacts) and Tasks.
- Events need the person's unhashed `email`. Events without one are listed in `failures` with code `INVALID_EVENT`.

## Forward conversions to Salesforce

Add a forwarding rule whose `destinationId` is the connection's org id, as listed by `GET /v1/ads/conversions/destinations`:

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">

```javascript
await fetch('https://zernio.com/api/v1/events/forwarding-rules', {
  method: 'POST',
  headers: { Authorization: `Bearer ${process.env.ZERNIO_API_KEY}`, 'Content-Type': 'application/json' },
  body: JSON.stringify({ profileId: 'PROFILE_ID', accountId: 'SALESFORCE_ACCOUNT_ID', destinationId: 'ORG_ID', eventNames: ['Purchase', 'Lead'] }),
});
```

</Tab>
<Tab value="Python">

```python
import os, requests

requests.post(
    "https://zernio.com/api/v1/events/forwarding-rules",
    headers={"Authorization": f"Bearer {os.environ['ZERNIO_API_KEY']}"},
    json={"profileId": "PROFILE_ID", "accountId": "SALESFORCE_ACCOUNT_ID", "destinationId": "ORG_ID", "eventNames": ["Purchase", "Lead"]},
)
```

</Tab>
<Tab value="curl">

```bash
curl -X POST "https://zernio.com/api/v1/events/forwarding-rules" \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "profileId": "PROFILE_ID", "accountId": "SALESFORCE_ACCOUNT_ID", "destinationId": "ORG_ID", "eventNames": ["Purchase", "Lead"] }'
```

</Tab>
</Tabs>

In the dashboard, the same rule is **Ads > Conversions > Destinations > Salesforce**.

## How it behaves

- The person is searched by email among Contacts and unconverted Leads. Without a match, one is created the way the connection creates people ([Settings](/platforms/salesforce/settings), a Lead by default) with the event's name and phone; without a last name, the email stands in for it.
- The Task's subject is the event name, its description lists the value and currency, the action source, the campaign, the page, the order id and the event id, and its date is the event's day.
- Events of a person new to Salesforce create one Lead, not two, even when they arrive at the same time.
- Events whose source is Salesforce (closed-won Opportunities) are never sent back to Salesforce, even from a rule that lists every source.
- Salesforce has no test mode: an event sent with `testCode` is not logged (code `TEST_MODE_UNSUPPORTED`).
- When Salesforce is down or the org's daily API allowance is spent before the Task is written, the events are sent again later; a failed Task write is not, so a Task is never logged twice.

## If it fails

| Error | Cause | Fix |
|---|---|---|
| `Salesforce: insufficient access rights` | The connected user cannot create Leads or Tasks | Grant Create on Leads and Tasks, or reconnect with another user |
| `Salesforce refused this connection's token` | Access was revoked in Salesforce | Reconnect Salesforce |
| `Salesforce API budget for this connection is spent` | The org's API allowance is reached | Nothing: the events are sent again later |
| `INVALID_EVENT` | The event has no unhashed email | Send `user.email` |

## Related

- [Add a forwarding rule](/conversions/create-forwarding-rule)
- [Send events](/conversions/send-events)
- [Opportunities](/platforms/salesforce/opportunities)