Sync a lead to the client's system
Requirement
"Whenever the agent writes a lead, it must appear in our system."
Pieces
- Custom data: a collection
leads. The agent writes leads with itswrite_leadstool (write_tool_idon the collection), attached to a node the way any tool is. - Connection: the CRM's key, provider
http_key. - One tool per action: an HTTP tool that posts one lead to the CRM.
- Flow: read the record that was written, then post it.
- Automation: trigger
record_writtenon the collection, running the flow.
The shape to avoid
A tool the agent calls that writes the lead and posts it to the CRM. A lead written any other way, by an import, by a person, by another agent, never reaches the CRM; a CRM outage loses the lead entirely; and the key ends up on the tool. Here the collection is the record of truth and the automation is what keeps the CRM in step, whoever wrote the row.
Steps
Ids from an earlier step are written as {{step_N.field}}; the test that runs
this page substitutes them.
-
The CRM's key, once.
create_connectionCode -
The leads. Name, phone and email are personal, so the retention window applies to them.
create_collectionCode -
One tool that posts one lead. The parameters become the JSON body.
create_toolCode -
The flow.
create_flowCode -
A
record_writtenrun starts withcollection_id,record_idandwritten_at, not with the record's values. So the entry step reads the record, with the collection's ownget_leadstool: a collection with no identity field looks up byrecord_id.add_step(flow_id={{step_4.id}})Code -
Then post it. A get step answers
{"record": {...}}with the record's fields, so each value is$ref:<step>.record.<field>.add_step(flow_id={{step_4.id}})Code -
Read, then post.
add_flow_edge(flow_id={{step_4.id}})Code -
The trigger. Created switched off.
min_interval_secondsis the floor between two runs of this automation.create_automationCode -
Switch it on. Enabling has its own route on purpose: it is the consequential step.
set_automation_enabled(automation_id={{step_8.id}})Code
What the agent sees
Nothing new. The agent writes a lead with write_leads as before; the
automation runs with no conversation, once per record written, and each run
is listed by list_automation_runs with what it was handed and how it ended.
Variations
- Only leads with a phone number: add a conditional edge between step 5
and step 6 with
{"field": "<step 5 id>.record.phone", "op": "exists"}. - An update in the CRM rather than a create: a second HTTP tool with
PUT, and a first HTTP tool that looks the lead up; the edge on whether it was found picks one. - The other direction, CRM to platform: an automation with
trigger_kind: "webhook"; the CRM calls the webhook and the flow's entry step reads$ref:trigger.<key>from the delivered body, writing withwrite_leads.