Collect Customer Information to Create a Record
Reliably collect customer input across multiple turns, then create a Salesforce record. Ensure that the agent collects all required information before creating the record. This example ensures the agent:
- Creates a record only after collecting all the required information.
- Reliably stores all provided information, even after multiple turns.
- Creates only one (not multiple) record per conversation.
- Confirms that a record creation actually happened.
A common agent pattern is to collect a lot of information from a customer, ask clarifying questions, verify the information is correct, and then create a Salesforce record. During multiple turns and long conversations, some agents can drop captured values, re-ask for information they already have, create half-filled records, or falsely confirm a record creation.
One solution is to create two subagents. (One subagent)[#add-the-lead-gather-subagent] gathers customer information and stores the information in variables. The subagent’s after_reasoning block checks whether all variables have values - if so, it runs the action to create the record. If the record ID is returned (indicating the record was created), the after_reasoning block transitions to a second subagent, which reports the record creation to the customer.
Create a flow that creates a lead record with the customer-provided fields.

-
From the app launcher, enter flows, and then select Flows.
-
Click New Flow.
-
Select the Autolaunched category, and then select Autolaunched Flow (No Trigger).
Expand the toolbox (button on the far left). Using the New Resource button, create six variables defined as follows. Match the API names exactly — they map to the agent action’s inputs and outputs.
| Resource Type | API Name | Data Type | Available for input | Available for output |
|---|---|---|---|---|
| Variable | Company | Text | select | don’t select |
| Variable | Email | Text | select | don’t select |
| Variable | FirstName | Text | select | don’t select |
| Variable | LastName | Text | select | don’t select |
| Variable | Phone | Text | select | don’t select |
| Variable | leadRecordId | Text | don’t select | select |
Click Save. Name the flow Create Lead by Field and ensure the flow’s API name is Create_Lead_by_Field.
We use this flow name later in the agent script. It needs to match, otherwise you’ll have to edit your script by hand.
Before creating a new lead record, we’ll use the provided email and company name to see if that lead record exists.
-
Add a Get Records Element.
- For the label, enter Get Existing Lead.
- For the object, select Lead.
-
Under Filter Lead Records, select All Conditions Are Met (AND), then enter these values:
Field Operator Value Email Equals Email Company Equals Company -
Under Sort Lead Records, for Sort Order, select Not Sorted.
-
Under How Many Records to Store, select Only the first record.
-
Under How to Store Record Data, select Choose fields and let Salesforce do the rest.
-
Under Select Lead Fields to Store in Variable, leave the first Field set to Id. You don’t need to add additional fields — the flow only uses the lead Id to detect an existing record.

Branch the flow based on whether Get Existing Lead returned a matching lead.
-
Add a Decision element after Get Existing Lead.
- For the label, enter Lead Exists?. The API Name auto-fills as
Lead_Exists.
- For the label, enter Lead Exists?. The API Name auto-fills as
-
Under Select Decision Logic, select Define Manually (Default).
-
Under Outcomes, configure the Yes outcome to run when a matching Lead was found.
-
For Outcome Label, enter Yes.
-
For Outcome API Name, enter Yes_Reuse.
-
For Condition Requirements to Execute Outcome, select All Conditions Are Met (AND).
-
Add this condition:
Resource Operator Value Get Existing Lead > Lead ID Is Null False
-
-
Select the default tab and rename it to No. The flow follows the No path when no matching lead exists.

On the Yes path, you’ll copy the existing Lead’s Id into leadRecordId so the flow returns the same value whether the lead was found or newly created.
-
On the Yes outcome from Lead Exists?, add an Assignment element.
- For the label, enter Assign Existing LeadId. The API Name auto-fills as
Assign_Existing_LeadId. - Under Set Variable Values, add this assignment:
Variable Operator Value leadRecordIdEquals Get Existing Lead > Lead ID - For the label, enter Assign Existing LeadId. The API Name auto-fills as
-
Connect the Assignment element to its own End element, which terminates the decision’s Yes path.

To make sure your agent gets credit for this lead, we’ll add LeadExampleAgent to the possible lead source values.
- From Setup, click Object Manager.
- Select Lead, then click Fields & Relationships.
- Click Lead Source.
- Under Account/Lead Source Picklist Values, click New, add
LeadExampleAgent. - Click Save.

Add an element to create the lead, mapping the agent action’s values to the variables you created earlier. Match the API names exactly — they map to the agent action’s inputs.
-
On the No outcome from Lead Exists?, add a Create Records element.
- For the label, enter Create Lead. The API Name auto-fills as
Create_Lead.
- For the label, enter Create Lead. The API Name auto-fills as
-
For How to set record field values, select Manually.
-
Under Create a Record of This Object, for Object, select Lead.
-
Under Set Field Values for the lead, add a row for each of these fields and map it to the indicated variable.
Field Value Company CompanyEmail EmailFirst Name FirstNameLast Name LastNameLead Source LeadExampleAgentPhone Phone -
Select Manually assign variables (advanced).
-
Under Store Lead ID in Variable, for Variable, select
leadRecordId. -
Leave Check for Matching Records disabled — the Lead Exists? decision already handles the duplicate check.
-
Click Save.

If the Create Lead element fails at runtime (for example, a validation rule rejects the record), the flow returns a lead Id anyway unless you explicitly clear it. Add a fault path that resets leadRecordId so the agent’s after_reasoning gate treats the create as unsuccessful.
-
Hover over the Create Lead element so the three dots appear.
-
Click the three dots and select Add Fault Path.
-
On the fault path, add an Assignment element.
- For the label, enter Clear LeadId On Fault. The API Name auto-fills as
Clear_LeadId_On_Fault.
- For the label, enter Clear LeadId On Fault. The API Name auto-fills as
-
Under Set Variable Values, add this assignment:
Variable Operator Value leadRecordIdEquals Blank Value (Empty String) -
Connect the Assignment element to an End element to terminate the fault path.

Test the flow.
- In Flow Builder, click Debug.
- Enter test values for
Company,Email,FirstName,LastName,Phone. - Click Run and verify a lead is created (or the existing one is returned). Notice that the lead source is
LeadExampleAgent.
Click Activate in the upper right.
You can create a new agent using the Agentforce Service Agent template, or you can add these subagents and variables to an existing agent.
For this example, you can add the two subagents to an existing agent. Or, you can create a service agent from a template. To create a service agent:
- In the App menu, enter and select Agentforce Builder.
- Select New Agent, then select Agentforce Service Agent.
- Give your agent a name, such as Lead Gather Example.
- Accept New User to create a new agent user.
- Switch to Script view (click the
</>toggle in the upper left)
The agent uses variables to store customer input during the information-collection turns. The agent also needs a variable to hold the lead ID that the action returns.
- Copy and paste these variables into your agent’s existing
variablesblock.
- Save your agent.
The variable defaults matter. Our agent uses "" to mean “not yet captured.”
The Lead Gather subagent asks for the required information and captures any relevant information that is provided. The after_reasoning block, which runs after every reasoning loop, checks if all variables are populated. Once all the variables have values (this might take many conversational turns), the agent runs the Create Lead_by_Field action to create the lead. If the action returns a lead record ID, the after_reasoning section transitions to the lead confirmation subagent.
- Copy this subagent script and paste the script at the end of your existing agent’s script. If you followed the flow naming conventions, this subagent correctly uses the Create Lead by Field action.
- Add these lines to the agent router, lining up the action with the other agent router actions.
- Save your agent.
You’ll see an error that the lead_gather subagent doesn’t exist - we’ll fix that problem in the next step.

This subagent tells the customer that a lead has been created. The agent router doesn’t have access to this subagent. This subagent is only called from the after_reasoning block of the create lead subagent, which is only available if the Create Lead by Field Action returned a lead Id. These safeguards ensure the agent never falsely confirms a lead creation to the customer.
- Copy this subagent script and paste the script at the end of your existing agent’s script.
- Save your agent.
Your agent user needs permission to read and create leads.
- From Setup, in the Quick Find box, enter and select Permission Sets.
- Click to open your agent user’s permission set.
- Click to open Object Settings.
- Scroll down, then click to open Leads.
- Click Edit.
- Under Object Permissions, for Read and Create, select Enabled.
- Click Save.

Always give your agent user the fewest permissions it needs to do its job. For more information about service agent permissions, see (Help:) Best Practices for Agent User Permissions.
Preview the agent and test three scenarios:
| Scenario | Customer Utterance | Expected Result |
|---|---|---|
| Happy path — all fields in one message. | Hi, I'd like to schedule a meeting to learn more about your product. Then when asked: I'm Casey Rivera, casey.rivera@example.com, Northwind Labs, 415-555-0142. | The agent transitions to lead_confirmation, creates the lead, and confirms the lead creation. |
| No duplicate record created. | Repeat the happy path with the same email and company. | The agent returns the same Lead ID as before. No duplicate lead record is created. |
| Partial-info gate — only some fields. | Hi, I want to book a meeting. Then when asked for information, provide only one answer per turn. | The agent keeps asking for the missing information. Once all information is provided, the agent creates the lead and confirms with the customer. |