salesforce_upsert_lead_from_linkedin
Salesforce write high risk
Part of the Lead Sync Agent.
What it does
Consent-gated, idempotent upsert of a single LinkedIn-sourced Lead into the client's Salesforce by an external-Id custom field (e.g. LinkedIn_Lead_Id__c), so repeated nightly syncs create-or-update exactly one Lead per LinkedIn lead with no duplicates. Two-phase write (Scalix guardrail, ADR-028/029): the first call (no preview_id) echoes the exact payload as the preview and returns a preview_id WITHOUT calling Salesforce - no dedupe, diff or picklist validation happens at preview; re-call with that preview_id and identical arguments to COMMIT: the request executes, Salesforce's response is returned, and an immutable audit row is written (single-use token, 30 min; a changed, forged or replayed token fails closed). Key inputs: external_id_field, external_id_value, fields{}, consent_confirmed, preview_id.
When to use it
Use for the nightly LinkedIn->Salesforce lead sync and any repeatable forward of a LinkedIn Lead Gen Form submission: it creates-or-updates exactly one Lead keyed by the LinkedIn external id, so re-running never duplicates. Always confirm consent and preview the create-vs-update outcome before approving. Prefer this over salesforce_create_lead whenever a stable LinkedIn external id exists. Never use it to load leads the client does not own or did not obtain consent for.
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
external_id_field | string | yes | API name of the external-Id custom field that keys the upsert (must be marked External ID + Unique in Salesforce), e.g. 'LinkedIn_Lead_Id__c'. |
external_id_value | string | yes | The LinkedIn lead's stable identifier for that field (e.g. the LinkedIn lead/response id). URL-encode in the path. This is the dedupe key — one Lead per value. |
fields | object | yes | Lead field map to set on create-or-update. REQUIRED on create: LastName, Company. Common: Email, FirstName, Title, Phone, Status, LeadSource ('LinkedIn'), plus mapped LinkedIn Lead Gen Form fields. Do NOT include the external_id_field in the body (it's in the path). Hash Email/Phone where the destination supports hashed matching. |
consent_confirmed | boolean | yes | MUST be true to commit: asserts the lead is the client's OWN lead and the client has a lawful basis/consent to store this PII. The commit is hard-blocked if false/absent. |
preview_id | string | no | Omit it to preview: nothing is applied, and the call returns a preview_id bound to this exact request. Pass it only after the user approves that preview in a later message, with the same arguments (leave out validate_only, and add any confirmation flag the preview names). Single-use, expires in 30 minutes; changed arguments need a fresh preview. |
Safeguards
A change in two steps. The first call previews and changes nothing; Claude shows you the preview and applies it only after you approve, within 30 minutes and with the same arguments. Every applied change goes to the audit log. How changes are approved.
Policy
CONSENT-GATED: commit is hard-blocked unless consent_confirmed=true. LinkedIn lead/member data may be used ONLY for the authorizing client and ONLY for consented purposes — forward only the client's OWN leads; NEVER use member data for prospecting, enrich/append/verify profiles, build a cross-account/unaffiliated lead database, or commingle data across clients. Hash PII (email/phone) where the destination supports hashed matching. Two-phase: preview (consent + create/update determination) then commit only on explicit approval. Strict per-account isolation — upsert only into this client's own org. Honor deletion on request; do not cache LinkedIn member profile data beyond 24h or social activity beyond 48h.
Reminder
consent_confirmed=true required; idempotent upsert by external Id (no duplicates); forward only the client's OWN leads, never enrich or pool across clients.
Errors
| Error class | Means | What to do | Retry |
|---|---|---|---|
CONSENT_REQUIRED | consent_confirmed was not true — upsert hard-blocked before any Salesforce call. | Confirm the lead is the client's own and consent/lawful basis exists, set consent_confirmed=true, then re-preview. Never bypass. | no |
TOKEN_REVOKED | Session expired/invalid (HTTP 401 INVALID_SESSION_ID). | needs_reauth — refresh once and retry; upsert by external Id is idempotent so retry is safe. Else prompt reconnect. | yes |
CONFLICT | Multiple existing records share the external_id_value (HTTP 300 Multiple Choices) — the external field is not actually unique. | Surface the returned candidate Ids; the external_id_field must be Unique. Resolve duplicates in Salesforce; do not force the write. | no |
VALIDATION_ERROR | Missing/invalid field (HTTP 400 REQUIRED_FIELD_MISSING for LastName/Company on create, NOT_FOUND/INVALID_FIELD if external_id_field isn't an External ID field, FIELD_CUSTOM_VALIDATION_EXCEPTION, or external_id_field wrongly included in the body). | Surface errors[].message + fields[]; ensure the external field is marked External ID + Unique and the body omits it; re-preview. | no |
PERMISSION_DENIED | User lacks Create/Edit on Lead or the external/field access (HTTP 403 INSUFFICIENT_ACCESS). | Request access; not retryable as-is. | no |
RATE_LIMITED | API limit exceeded (HTTP 403 REQUEST_LIMIT_EXCEEDED or 503 Retry-After) — common on large nightly syncs. | Backoff, honor Retry-After, throttle the sync batch; report remaining quota. Resume rather than restart. | yes |
PREVIEW_EXPIRED | preview_id unknown/expired or inputs changed since preview. | Re-run preview before committing. | no |
PLATFORM_UNAVAILABLE | Salesforce 5xx (HTTP 500/502/503). | Retry with backoff; upsert by external Id is idempotent so a safe re-PATCH will not duplicate. | yes |
Edge cases
- Idempotent upsert: re-running with the same external_id_value updates the same Lead — the core anti-duplicate guarantee for nightly sync.
- Create path returns 201 with {id,created:true}; update path returns 204 No Content (created:false) — surface which happened.
- HTTP 300 means the external field is not unique (multiple matches) — must be fixed in Salesforce config, not worked around.
- external_id_field must be configured as External ID + Unique; otherwise the resource 404s/validation-errors.
- Do not put the external_id_field in the request body (it lives in the URL path) — Salesforce rejects the duplicate.
- Partial-batch on a multi-lead sync: process per-lead, record per-record success/failure, and resume from the last good external id rather than restarting (avoids re-processing).
- Consent/retention: forward only the client's OWN consented leads; on a deletion request, delete the synced Lead; never retain LinkedIn profile cache beyond 24h or social activity beyond 48h; never use the upserted data to enrich or build a cross-client database.