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.

The Problem 

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.

The Solution 

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 to Create the Lead Record from Fields 

Create a flow that creates a lead record with the customer-provided fields.

Flow overall

Step 1 - Create the Autolaunched Flow 

  1. From the app launcher, enter flows, and then select Flows.

  2. Click New Flow.

  3. Select the Autolaunched category, and then select Autolaunched Flow (No Trigger).

Step 2 - Create the Flow Variables 

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 TypeAPI NameData TypeAvailable for inputAvailable for output
VariableCompanyTextselectdon’t select
VariableEmailTextselectdon’t select
VariableFirstNameTextselectdon’t select
VariableLastNameTextselectdon’t select
VariablePhoneTextselectdon’t select
VariableleadRecordIdTextdon’t selectselect

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.

Important

Step 3 - Get Existing Lead 

Before creating a new lead record, we’ll use the provided email and company name to see if that lead record exists.

  1. Add a Get Records Element.

    1. For the label, enter Get Existing Lead.
    2. For the object, select Lead.
  2. Under Filter Lead Records, select All Conditions Are Met (AND), then enter these values:

    FieldOperatorValue
    EmailEqualsEmail
    CompanyEqualsCompany
  3. Under Sort Lead Records, for Sort Order, select Not Sorted.

  4. Under How Many Records to Store, select Only the first record.

  5. Under How to Store Record Data, select Choose fields and let Salesforce do the rest.

  6. 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.

get records element to get matching record

Step 4 - Check Whether the Lead Exists 

Branch the flow based on whether Get Existing Lead returned a matching lead.

  1. Add a Decision element after Get Existing Lead.

    1. For the label, enter Lead Exists?. The API Name auto-fills as Lead_Exists.
  2. Under Select Decision Logic, select Define Manually (Default).

  3. Under Outcomes, configure the Yes outcome to run when a matching Lead was found.

    1. For Outcome Label, enter Yes.

    2. For Outcome API Name, enter Yes_Reuse.

    3. For Condition Requirements to Execute Outcome, select All Conditions Are Met (AND).

    4. Add this condition:

      ResourceOperatorValue
      Get Existing Lead > Lead IDIs NullFalse
  4. Select the default tab and rename it to No. The flow follows the No path when no matching lead exists.

decision element to see if the lead exists

Step 5 - Assign Existing Lead Id 

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.

  1. On the Yes outcome from Lead Exists?, add an Assignment element.

    1. For the label, enter Assign Existing LeadId. The API Name auto-fills as Assign_Existing_LeadId.
    2. Under Set Variable Values, add this assignment:
    VariableOperatorValue
    leadRecordIdEqualsGet Existing Lead > Lead ID
  2. Connect the Assignment element to its own End element, which terminates the decision’s Yes path.

decision element no path

Step 6 - Add LeadExampleAgent to the LeadSource Picklist 

To make sure your agent gets credit for this lead, we’ll add LeadExampleAgent to the possible lead source values.

  1. From Setup, click Object Manager.
  2. Select Lead, then click Fields & Relationships.
  3. Click Lead Source.
  4. Under Account/Lead Source Picklist Values, click New, add LeadExampleAgent.
  5. Click Save.

decision element to see if the lead exists

Step 7 - Create the Lead 

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.

  1. On the No outcome from Lead Exists?, add a Create Records element.

    1. For the label, enter Create Lead. The API Name auto-fills as Create_Lead.
  2. For How to set record field values, select Manually.

  3. Under Create a Record of This Object, for Object, select Lead.

  4. Under Set Field Values for the lead, add a row for each of these fields and map it to the indicated variable.

    FieldValue
    CompanyCompany
    EmailEmail
    First NameFirstName
    Last NameLastName
    Lead SourceLeadExampleAgent
    PhonePhone
  5. Select Manually assign variables (advanced).

  6. Under Store Lead ID in Variable, for Variable, select leadRecordId.

  7. Leave Check for Matching Records disabled — the Lead Exists? decision already handles the duplicate check.

  8. Click Save.

decision element to see if the lead exists

Step 8 - Handle a Create Lead Fault 

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.

  1. Hover over the Create Lead element so the three dots appear.

  2. Click the three dots and select Add Fault Path.

  3. On the fault path, add an Assignment element.

    1. For the label, enter Clear LeadId On Fault. The API Name auto-fills as Clear_LeadId_On_Fault.
  4. Under Set Variable Values, add this assignment:

    VariableOperatorValue
    leadRecordIdEqualsBlank Value (Empty String)
  5. Connect the Assignment element to an End element to terminate the fault path.

decision element to see if the lead exists

Step 9 - Test the Flow 

Test the flow.

  1. In Flow Builder, click Debug.
  2. Enter test values for Company, Email, FirstName, LastName, Phone.
  3. Click Run and verify a lead is created (or the existing one is returned). Notice that the lead source is LeadExampleAgent.

Step 10 - Activate Your Flow 

Click Activate in the upper right.

Create Your Agent 

You can create a new agent using the Agentforce Service Agent template, or you can add these subagents and variables to an existing agent.

Step 1 - Create or Reuse an 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:

  1. In the App menu, enter and select Agentforce Builder.
  2. Select New Agent, then select Agentforce Service Agent.
  3. Give your agent a name, such as Lead Gather Example.
  4. Accept New User to create a new agent user.
  5. Switch to Script view (click the </>toggle in the upper left)

Step 2 - Create the Variables 

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.

  1. Copy and paste these variables into your agent’s existing variables block.
Lead Gather Variables
1# Variables from Lead Gather example
2# variables:
3    lead_id: mutable string = ""
4        description: "Stores the record ID of the Lead created for the prospect."
5        visibility: "Internal"
6    company: mutable string = ""
7        description: "Stores the prospect's company name."
8        visibility: "Internal"
9    email: mutable string = ""
10        description: "Stores the prospect's email address."
11        visibility: "Internal"
12    first_name: mutable string = ""
13        description: "Stores the prospect's first name."
14        visibility: "Internal"
15    last_name: mutable string = ""
16        description: "Stores the prospect's last name."
17        visibility: "Internal"
18    phone: mutable string = ""
19        description: "Stores the prospect's phone number."
20        visibility: "Internal"
  1. Save your agent.

The variable defaults matter. Our agent uses "" to mean “not yet captured.”

Note

Add the Lead Gather Subagent 

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.

  1. 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.
lead_gather Subagent
1subagent lead_gather:
2  label: "Lead Gather"
3  description: "Collects prospect details and creates a lead."
4  reasoning:
5      instructions: ->
6          | Ask for first name, last name, email, company, and phone number in one message.
7          | Use {!@actions.capture_details} whenever the prospect supplies details.
8          | Ask only for any remaining missing details.
9          | Do not claim that the lead was created or that a meeting was scheduled.
10      actions:
11          capture_details: @utils.setVariables
12              # this action is available ONLY if the lead hasn't been created yet this session
13              # That is, the customer can't create a second unique lead in the same session
14              available when @variables.lead_id == ""
15              with company = ...
16              with email = ...
17              with first_name = ...
18              with last_name = ...
19              with phone = ...
20  # after_reasoning is run after every reasoning session
21  after_reasoning:
22
23      # if we DON'T have all the variables we need, don't run the action
24      if @variables.company != "" and @variables.email != "" and @variables.first_name != "" and @variables.last_name != "" and @variables.phone != "" and @variables.lead_id == "":
25          run @actions.Create_Lead_by_Field
26              with Company = @variables.company
27              with Email = @variables.email
28              with FirstName = @variables.first_name
29              with LastName = @variables.last_name
30              with Phone = @variables.phone
31              set @variables.lead_id = @outputs.leadRecordId
32
33      # if we DID create a lead, transition to the Lead Confirmation subagent
34      if @variables.lead_id != "":
35          transition to @subagent.lead_confirmation
36  actions:
37      Create_Lead_by_Field:
38          description: "Creates a Lead record from the prospect details captured in the conversation."
39          label: "Create Lead by Field"
40          require_user_confirmation: False
41          include_in_progress_indicator: True
42          target: "flow://Create_Lead_by_Field"
43          inputs:
44              "Company": string
45                  description: "The prospect's company name."
46                  label: "Company"
47                  is_required: True
48                  is_user_input: False
49              "Email": string
50                  description: "The prospect's email address."
51                  label: "Email"
52                  is_required: True
53                  is_user_input: False
54              "FirstName": string
55                  description: "The prospect's first name."
56                  label: "First Name"
57                  is_required: True
58                  is_user_input: False
59              "LastName": string
60                  description: "The prospect's last name."
61                  label: "Last Name"
62                  is_required: True
63                  is_user_input: False
64              "Phone": string
65                  description: "The prospect's phone number."
66                  label: "Phone"
67                  is_required: True
68                  is_user_input: False
69          outputs:
70              "leadRecordId": string
71                  description: "The Salesforce record ID of the newly created Lead."
72                  label: "Lead Record Id"
73                  is_displayable: False
74                  filter_from_agent: True
  1. Add these lines to the agent router, lining up the action with the other agent router actions.
lead_gather Subagent
1go_to_lead_gather: @utils.transition to @subagent.lead_gather
  1. 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.

Note

decision element to see if the lead exists

Add the Lead Confirmation Subagent 

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.

  1. Copy this subagent script and paste the script at the end of your existing agent’s script.
lead_confirmation Subagent
1subagent lead_confirmation:
2  label: "Lead Confirmation"
3  description: "Confirms that the lead was successfully created."
4  reasoning:
5      instructions: ->
6          | Confirm that the prospect's details were captured successfully.
7          | Explain that a team member will follow up to arrange a meeting.
  1. Save your agent.

Grant Agent User Permissions 

Your agent user needs permission to read and create leads.

  1. From Setup, in the Quick Find box, enter and select Permission Sets.
  2. Click to open your agent user’s permission set.
  3. Click to open Object Settings.
  4. Scroll down, then click to open Leads.
  5. Click Edit.
  6. Under Object Permissions, for Read and Create, select Enabled.
  7. Click Save.

the read and create permissions selected

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.

Note

Test Your Agent 

Preview the agent and test three scenarios:

ScenarioCustomer UtteranceExpected 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.