Prompt
How do I automate lead handoff from my app to Salesforce when a lead is created?
Latest observation
The cleanest way is to trigger a Salesforce Lead create or upsert from your app whenever a new lead is created, usually via Salesforce’s REST API. Here’s the practical pattern:
1) Decide the integration flow
Common options:
- Direct API call from your app: your backend sends the lead to Salesforce immediately after creation.
- Webhook + integration service: your app emits a webhook, and a worker or middleware (Zapier, Workato, Mulesoft, Celigo, etc.) handles Salesforce.
- Queue-based async: your app publishes a “lead created” event, and a background worker syncs it to Salesforce.
For most production apps, the best approach is:
- create the lead in your app,
- enqueue a job,
- asynchronously upsert it into Salesforce.
This avoids slowing down user requests and makes retries easier.
2) Create a Salesforce integration user / connected app
You’ll need Salesforce auth set up.
Typical choices:
- OAuth 2.0 with a Connected App
- JWT Bearer Flow for server-to-server integrations
- Username-password flow only for quick testing; not ideal for production
Use a dedicated integration user with least-privilege access.
3) Map your fields to Salesforce Lead fields
Example mapping:
| Your app | Salesforce Lead |
|---|---|
| first_name | FirstName |
| last_name | LastName |
| phone | Phone |
| company | Company |
| source | LeadSource |
| status | Status |
| notes | Description |
Important: Salesforce Lead usually requires at least:
LastNameCompany
If you create leads for individuals without a company, you may need a placeholder like "Individual" or use your org’s custom configuration.
4) Use Salesforce REST API to create or upsert the lead
Create a lead
POST /services/data/vXX.X/sobjects/Lead/
Example JSON:
{
"FirstName": "Jane",
"LastName": "Doe",
"Company": "Acme Inc",
"Email": "jane@example.com",
"Phone": "+1-555-123-4567",
"LeadSource": "Website",
"Status": "New"
}
Upsert a lead to prevent duplicates
If you have a unique external ID in your app, create a custom field in Salesforce like External_Id__c and use:
PATCH /services/data/vXX.X/sobjects/Lead/External_Id__c/{yourExternalId}
This is usually better than blindly creating new records.
5) Handle lead assignment
Once the lead is created in Salesforce, you can let Salesforce assign it automatically using:
- Lead Assignment Rules
- Apex triggers / Flows
- Territory rules or custom routing logic
If you want Salesforce to apply an assignment rule on insert, include the assignment rule header or create a Flow/automation in Salesforce.
6) Store Salesforce IDs back in your app
After successful creation, save:
- Salesforce Lead ID
- sync status
- timestamp
- error details if any
This helps with:
- updates
- deduplication
- support/debugging
7) Add retry/error handling
You should expect:
- auth failures
- validation rule failures
- rate limits
- temporary Salesforce outages
Best practices:
- retry transient errors with exponential backoff
- log request/response payloads safely
- dead-letter failed jobs for manual review
8) Example backend pseudo-code
def on_lead_created(lead):
payload = {
"FirstName": lead.first_name,
"LastName": lead.last_name or "Unknown",
"Company": lead.company or "Individual",
"Email": lead.email,
"Phone": lead.phone,
"LeadSource": "Website",
"Status": "New"
}
sf_response = salesforce_client.create_lead(payload)
if sf_response.success:
save_salesforce_id(lead.id, sf_response.id)
else:
log_error(lead.id, sf_response.error)
raise RetryableSyncError()
9) If you want real-time but more robust automation
A good architecture is:
- Lead created in app
- App writes lead to DB
- App emits
lead.createdevent - Worker consumes event
- Worker upserts into Salesforce
- Worker updates sync status
This gives you:
- resilience
- auditability
- easier retries
- no user-facing delay
10) If you need bi-directional sync
If Salesforce should also update your app:
- use Salesforce Platform Events
- Outbound Messages
- Change Data Capture
- scheduled polling if necessary
If you want, I can give you:
- a Node.js / Python / Java example for the Salesforce API call, or
- a no-code option using Zapier/Make, or
- the exact Salesforce auth setup steps for a Connected App.