Introducing a new way to review your PRs.

Read the announcement

frappe / crm

Fully featured, open source CRM

View on GitHub

24 documents · Updated Nov 23, 2026

docs / lead-source-synchronization

Lead-source synchronization

Lead-source synchronization brings prospect submissions from an external lead system into CRM. It lets a sales manager connect a source to a form, map the form answers to CRM Lead fields, and run imports on demand or on a schedule while keeping duplicate and failed submissions visible.

System role

The subsystem turns external form submissions into CRM Leads. A configured source owns the connection details and the form choice, while the imported lead becomes part of the normal CRM sales flow. The current source type is Facebook.

Source configuration

Source configuration defines what external form to read and how often to read it. The configuration is saved as a Lead Sync Source record and can be managed by System Managers and Sales Managers.

Source records

Each source has an access token, a selected Facebook Page, a selected Facebook Lead Form, an enabled state, a background frequency, and a read-only last-sync time. The source name identifies the connection in the source list and in its failure logs. Only one enabled source can point to a given form, so one campaign does not have competing import owners.

The available schedule choices are:

  • Every 5 Minutes
  • Every 10 Minutes
  • Every 15 Minutes
  • Hourly
  • Daily
  • Monthly

References: crm/…/lead_sync_source.json• L20-83 crm/…/lead_sync_source.py• L33-49

Facebook form mapping

When a Facebook source is created, the system checks the access token and discovers the account's pages and their lead forms. It stores those pages and forms so the source can select them later. Each form keeps its external questions and a mapping from each question key to a CRM Lead field.

The mapping is the boundary between external data and CRM data. A form must map a question to first_name before it can be used, because CRM requires that field for a new lead. Unmapped questions are not copied into the CRM Lead.

References: crm/…/facebook.py• L142-226 crm/…/facebook_lead_form.py• L28-48 crm/…/facebook_lead_form_question.json• L16-50 frontend/…/LeadSyncSourceForm.vue• L111-153

Synchronization lifecycle

Synchronization reads new submissions from the selected form, converts the mapped answers into CRM fields, and stores each accepted submission as a CRM Lead. The process records enough source identity to connect the CRM record back to the external campaign.

Acquisition flow

The source uses the Facebook Graph API to request submissions newer than its saved sync time. For each submission, it applies the form mapping, sets the CRM lead source to Facebook, and stores the Facebook lead ID and form ID on the CRM Lead. After the pass, the source records a new sync time for the next request.

This diagram shows how source setup becomes a persisted CRM Lead or an operational log entry.

flowchart LR
    Token[Facebook access token] --> Discovery[Pages and lead forms]
    Discovery --> Mapping[Question to CRM field mapping]
    Mapping --> Source[Lead Sync Source]
    Source --> Run[Manual or scheduled run]
    Run --> API[New form submissions]
    API --> Transform[Map answers and identify source]
    Transform -->|new lead| Lead[CRM Lead]
    Transform -->|duplicate or error| Log[Failed Lead Sync Log]

References: crm/…/facebook.py• L20-66 crm/…/facebook.py• L68-85 crm/…/crm_lead.json• L356-374

Run modes

Users can start a source from its settings page. The normal user path starts the work in the background, so the page can return while external requests and lead creation continue. Enabled sources also run from scheduled jobs that select sources by their configured frequency. Disabled sources are not selected for those jobs.

The scheduler has a separate entry for each supported frequency. This keeps a source's saved choice aligned with the interval that runs it and lets multiple sources run under the same interval.

References: crm/…/lead_sync_source.py• L56-69 crm/…/background_sync.py• L4-37 crm/hooks.py• L221-241 frontend/…/LeadSyncSourceForm.vue• L21-39

Imported lead persistence

An accepted submission is stored as a normal CRM Lead rather than as a temporary sync result. This lets downstream sales work use the same lead record as leads entered through other CRM paths.

Lead identity

The imported lead records the external Facebook lead ID and form ID in the CRM Lead syncing fields. The lead also receives the Facebook value in the CRM lead-source field. These values preserve the link to the campaign and support duplicate checks within that form.

The duplicate boundary uses the mapped CRM values together with the Facebook form ID. A matching submission is not inserted a second time. The external lead ID is also unique on the CRM Lead, which adds a record-level protection against repeated imports.

References: crm/…/facebook.py• L41-66 crm/…/facebook.py• L135-139 crm/…/crm_lead.json• L178-182 crm/…/crm_lead.json• L356-374

Failure records

The subsystem keeps a Failed Lead Sync Log for submissions that do not become leads. The log stores the source, the original lead payload, a reason, and an error trace when one exists. Duplicate submissions use the Duplicate reason. Other processing errors use Failure.

Operators can inspect these records from the source and retry a non-synced entry. A successful retry changes the log to Synced and returns the created CRM Lead. This gives the failed submission a visible recovery path without requiring a new external form submission.

References: crm/…/failed_lead_sync_log.json• L16-60 crm/…/failed_lead_sync_log.py• L25-40 frontend/…/FailureLogs.vue• L1-79

Operational behavior

The subsystem is designed to keep a source running when one submission has a problem. It isolates the bad submission in a log, continues the source pass, and exposes the saved sync time and logs for review.

Sync progress

The saved last-sync time acts as the source cursor. A later run asks Facebook for submissions after that time, which avoids reading the full form history on every run. The cursor advances after the source processes its fetched batch, including cases where individual submissions were written to a failure log.

This means a failed submission must be recovered from its failure log. Running the source again does not by itself guarantee that the same failed item will be fetched again.

References: crm/…/facebook.py• L35-39 crm/…/facebook.py• L68-85 crm/…/facebook.py• L108-127

Source administration

The settings view lists sources with their type and enabled state. Users can create, edit, duplicate, enable, disable, or delete a source. An edited source shows its last-sync time and its failure logs, which gives operators one place to manage the connection and review its results.

The current interface advertises Facebook as the supported source type. The source model leaves room for more types, but another source type does not have an acquisition or retry path today.

References: frontend/…/LeadSyncSources.vue• L4-25 frontend/…/LeadSyncSourceForm.vue• L43-159 frontend/…/leadSyncSourceConfig.js• L3-14 crm/…/lead_sync_source.py• L51-69

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the frequency labels in Lead Sync Source aligned with the scheduler entries. A label mismatch silently leaves a source out of its intended schedule.
  • Keep the Facebook question keys, CRM Lead field names, and the required first_name mapping rule aligned. A change in one can stop a form from producing valid leads.
  • Preserve the form ID and Facebook lead ID contract when changing CRM Lead fields or duplicate checks. These values define source identity and repeat protection.
  • Keep cursor updates and failure-log behavior aligned. Advancing the cursor changes how operators recover failed submissions.
  • If a source type is added, provide its discovery, lead import, background sync, and failure retry behavior together.

Important failure modes

TriggerConsequenceGuard
Facebook access token is missing or invalidPage discovery or lead fetch stops before importValidate the token during source setup and replace it when Facebook rejects it.
Form does not map a question to first_nameThe form cannot pass validation for use as a lead sourceMap the required form question to first_name.
Two enabled sources use the same Facebook formThe second source cannot be saved as an active sourceKeep one enabled source for each form.
A submission matches an existing lead for its formCRM does not create a second lead; it records a duplicate logReview the existing lead or the duplicate log.
A submission fails after it is fetchedThe lead is not created and the payload is stored in a failure logReview the log and use Retry Sync before discarding the entry.
A source is disabledScheduled jobs skip the sourceEnable the source before expecting background imports.

Key files

FileWhy to read it
crm/…/lead_sync_source.pyDefines source validation, manual sync entry, and the supported source boundary.
crm/…/facebook.pyDefines Facebook discovery, submission import, mapping, duplicate handling, cursor updates, and failure logs.
crm/…/lead_sync_source.jsonDefines the source configuration contract and schedule choices.
crm/…/facebook_lead_form.pyDefines the minimum form mapping needed to create CRM Leads.
crm/…/facebook_lead_form_question.jsonDefines the question-to-CRM field mapping data.
crm/…/background_sync.py and crm/hooks.pyDefine how enabled sources run at each background interval.
crm/…/failed_lead_sync_log.pyDefines the retry path for failed or duplicate submissions.
frontend/…/LeadSyncSourceForm.vueShows the operator flow for source setup, mapping, manual sync, and failure review.
frontend/…/FailureLogs.vueShows how operators inspect and retry failed submissions.