# Stream a Salesforce org

> Land a Salesforce org's accounts, contacts, leads, opportunities, cases and activities in your warehouse, every field included, through Salesforce's own sign-in.

A Salesforce stream lands a Salesforce org's accounts, contacts, leads, opportunities, cases, tasks, events, campaigns, products, price books, quotes, orders and the users behind them in your warehouse, as often as every 15. Each record lands with every field the user you connect as can see, custom fields included, and each run reads only what changed since the last run.

Salesforce is a **published source**, like [Square](https://docs.sanda-os.com.au/streams/square): Sanda wrote the code that reads it and runs it as its own, so there is no connector for Sam to write or for anyone to approve. You connect the org through Salesforce's own sign-in, choose what to read, and set a schedule.

:::note
Salesforce is in the catalogue once the Salesforce pull is switched on for your console, as a card under **Choose a source** that says **Accounts, pipeline, cases and activity**. A second **Salesforce** card that says **Syncs through a connection** is a nightly [Salesforce connection](https://docs.sanda-os.com.au/connectors/salesforce), which stays beside the pull: it is the way to bring in custom objects, which this pull doesn't read. If you only see that one, ask Sanda support to switch the pull on.
:::

## Before you start

- **Choose who Sanda reads as.** Sanda sees exactly what the Salesforce user you sign in as can see, object by object and field by field: a record or field that user can't see never lands. Use a Salesforce integration user (the API-only licence) with read access to what you want in your warehouse, rather than a person's own login, so the stream never depends on one person staying in the job.
- **Let that user allow Sanda.** If your org hasn't installed Sanda's app, Salesforce only lets a user allow it with the **Approve Uninstalled Connected Apps** permission. A Salesforce admin can give it to the integration user.
- **API access.** The org needs an edition with API access (Enterprise, Unlimited or Performance, or Professional with API access added), and the user needs **API Enabled** in its profile or a permission set.
- **One connection for Push and the pull.** An org you already connected for a [push](https://docs.sanda-os.com.au/streams/connect-salesforce) is the same connection here: it is listed under **Connected** already, and you can make a stream on it straight away.
- **In Sanda, only owners and admins connect an org** and make a stream on it, because Sanda reads it on the whole workspace's behalf.

## Connect an org

:::steps
1. **Open Pull.** Go to **Data · Streams**, open the **Pull** tab and press the **Salesforce** card under **Choose a source**. The new pull's editor opens on **Connect a Salesforce org** when none is connected yet.
2. **Say if it is a sandbox.** Tick **This is a sandbox org.** for a sandbox, which signs in at `test.salesforce.com`. Leave it unticked for production.
3. **Press Continue to Salesforce.**
4. **Sign in to Salesforce.** Salesforce asks which user to sign in as even when this browser is already signed in, because the org open in another tab is often not the one you mean. Sign in as the user Sanda should read as.
5. **Allow Sanda.** Salesforce shows what Sanda asks for: to manage data through the API, and to do so at any time. Press **Allow**.
6. **Back in Sanda**, a line at the top of the page says the org is connected, and it is listed under Salesforce in **Connected** by its name. If you started from a new stream, you are back in its editor with the org chosen.
:::

Sanda keeps the connection encrypted, and it is never shown, in the console or to Sam. Connecting uses Salesforce's sign-in with PKCE, so the code Salesforce hands back is useless to anyone but the Sanda tab that asked for it.

**Connect again** connects the org again, when Salesforce stops accepting Sanda's connection: an admin revoked it, a policy expired it, or the user was deactivated. Sign in to the same org (and tick **This is a sandbox org.** again for a sandbox), and every stream and push on it carries on.

**Disconnect** forgets the connection and revokes Sanda's access in Salesforce. Because Push and the pull share the connection, the org's pushes wait too, until it is connected again.

## Make a stream

:::steps
1. **Start it.** Press the **Salesforce** card under **Choose a source**, or **New pull** and then **Salesforce**.
2. **Choose when it runs.** **When it runs** comes first: how often, within which hours, on which days, with every run drawn out and the cost a month beneath. A Salesforce stream runs at most every 15, because each run spends calls from the org's daily API allowance. See [when a stream runs](https://docs.sanda-os.com.au/streams/schedules).
3. **Name it.** Its tables are named after it: a stream called `salesforce` lands accounts in `raw.salesforce__accounts`.
4. **Choose the org**, and, if your workspace has more than one warehouse, the warehouse it lands in. Both are chosen once.
5. **Choose what it reads.** Everything but **Quotes**, **Quote line items**, **Contracts**, **Orders** and **Order products** is ticked. Those five are features an org turns on, so tick them only if yours uses them. Untick what you don't need.
6. **Read history from** is optional: the first run reads every record changed since that date. Leave it empty to read every record the org has.
7. **Press Save pull.** Saving reads nothing yet: press **Run now**, or wait for its schedule.
:::

## What it reads

| Tick | Salesforce object | Lands in |
|---|---|---|
| **Accounts** | Account | `raw.<stream>__accounts` |
| **Contacts** | Contact | `raw.<stream>__contacts` |
| **Leads** | Lead | `raw.<stream>__leads` |
| **Opportunities** | Opportunity | `raw.<stream>__opportunities` |
| **Opportunity products** | OpportunityLineItem | `raw.<stream>__opportunity_line_items` |
| **Opportunity contact roles** | OpportunityContactRole | `raw.<stream>__opportunity_contact_roles` |
| **Opportunity history** | OpportunityHistory | `raw.<stream>__opportunity_history` |
| **Cases** | Case | `raw.<stream>__cases` |
| **Tasks** | Task | `raw.<stream>__tasks` |
| **Events** | Event | `raw.<stream>__events` |
| **Campaigns** | Campaign | `raw.<stream>__campaigns` |
| **Campaign members** | CampaignMember | `raw.<stream>__campaign_members` |
| **Products** | Product2 | `raw.<stream>__products` |
| **Price books** | Pricebook2 | `raw.<stream>__pricebooks` |
| **Price book entries** | PricebookEntry | `raw.<stream>__pricebook_entries` |
| **Users** | User | `raw.<stream>__users` |
| **Roles** | UserRole | `raw.<stream>__user_roles` |
| **Record types** | RecordType | `raw.<stream>__record_types` |
| **Quotes** | Quote | `raw.<stream>__quotes` |
| **Quote line items** | QuoteLineItem | `raw.<stream>__quote_line_items` |
| **Contracts** | Contract | `raw.<stream>__contracts` |
| **Orders** | Order | `raw.<stream>__orders` |
| **Order products** | OrderItem | `raw.<stream>__order_items` |

Each run reads the records of each object that changed since the last run, by Salesforce's own `SystemModstamp`, which moves on every change, including the ones Salesforce makes itself (a roll-up summary recalculated, say). Each table is keyed by the record's Salesforce `Id`. The editor marks the four that land people's names and contact details: contacts, leads, campaign members and users.

### Every field, as Salesforce names it

A record lands with every field the user you connect as can see, each as a column named as Salesforce names it: `Name`, `Industry`, `StageName`, and custom fields with their `__c`, such as `Region__c`. A field someone adds in Salesforce becomes a new column on the next run, with nothing to set in Sanda. An address lands as one column holding its parts (`BillingAddress`), beside the parts' own columns (`BillingCity`, `BillingPostalCode`). The whole record is kept in `_record` too.

### Deleted records

A record deleted in Salesforce lands again with `IsDeleted` set to true while it sits in the org's recycle bin, which Salesforce empties after 15. A stream that runs at least that often sees every delete; filter on `IsDeleted` in your models. Archived tasks and events land too.

### Custom objects

This pull reads the standard objects above, with their custom fields. To bring in custom objects (`Project__c` and the like), add a [Salesforce connection](https://docs.sanda-os.com.au/connectors/salesforce), which syncs nightly.

## Your API allowance

Every request a run makes is one call from the org's daily API allowance, which the org's other integrations, and its pushes, share. A request reads up to 200 records, so a first read of a large org spends the most, and later runs spend about one call per ticked object.

Sanda reads the allowance Salesforce reports with every answer. Once the org has used 80% of the day's allowance, the run lands the page it has, stops, and says so, leaving the rest for everything else. The next run carries on from where it stopped. If the org has already used its whole allowance, the run stops at once with the same advice.

## If a run stops

| What the run says | What to do |
|---|---|
| The org has used its API allowance, or 80% of it | Nothing: the next run carries on. To leave more room, run the stream less often or untick objects you don't need. |
| Salesforce didn't let Sanda read an object | The org doesn't use it (quotes and orders are features an org turns on), or the user Sanda reads as can't see it. The run carries on with the rest. Untick the object, or give that user access. |
| The org does not allow API access | The org's edition has no API access. It needs Enterprise, Unlimited or Performance, or Professional with API access added. |
| API access is switched off for the user | Turn on **API Enabled** in the user's profile or a permission set. |
| Salesforce no longer accepts Sanda's connection | Press **Connect again** beside the org under **Connected**. Its streams are skipped, not failed, until you do. |

For anything else, see [runs](https://docs.sanda-os.com.au/streams/runs).
