# Salesforce Integration

Sync contacts and deals between MailTrixy and Salesforce CRM.

## Setup

1. Admin configures OAuth: Admin → Settings → Integrations → Salesforce. Enter Connected App Consumer Key and Consumer Secret from Salesforce Setup.
2. User connects: Go to Settings → Integrations → click Connect on Salesforce. You will be redirected to Salesforce to authorize access.
3. Sync starts: Once connected, contacts and deals sync automatically.

## Features

- Contact Sync: Pull contacts from Salesforce into MailTrixy
- Deal Sync: Push deals from MailTrixy to Salesforce as Opportunities
- Two-way: Changes in either system are reflected
- Manual Sync: Click "Sync Now" in Settings → Integrations

## Creating a Salesforce Connected App

1. Log in to Salesforce → Setup → App Manager → New Connected App
2. Enable OAuth Settings
3. Set Callback URL to: `https://yourdomain.com/oauth/salesforce/callback`
4. Select scopes: `api`, `refresh_token`, `offline_access`
5. Copy the Consumer Key and Consumer Secret to MailTrixy admin settings

## Salesforce Card in the Inbox

When the Salesforce integration is active AND the conversation's contact email matches a Salesforce Contact, a Salesforce card appears in the right-side details panel of that conversation.

How to see it:

1. Open a conversation.
2. Click the right-panel toggle in the top-right of the conversation header.
3. The Salesforce card appears below the Contact card (and below the HubSpot and Stripe cards if those also match).

What the card shows:

- Account name — the Salesforce Account the contact belongs to.
- Title & Lead source — if populated on the Contact.
- Opportunities — up to 5 recent opportunities on the Account, each with name, stage, amount, and a pill: Won / Lost / Open.
- "Open in Salesforce" — deep-link to the Contact record in your Salesforce org using the saved `salesforce_instance_url`.

When the card does NOT appear:

- Salesforce integration is not connected for the workspace.
- The contact has no email.
- The contact's email is not in your Salesforce Contacts (checked by an exact-match SOQL `WHERE Email = '…'`).
- The Salesforce access token is expired and cannot be refreshed — reconnect the integration.

> Caching: the card is cached per-email for 5 minutes to avoid hitting Salesforce API limits. Two SOQL calls are made per lookup: one for the Contact, one for the Account's Opportunities.