Sub topic 7
Building the flow logic: Get Records, decisions, and updates
This chapter covers the middle of an action-ready autolaunched flow: the part that reads Salesforce data, branches on what it finds, and changes records. It assumes you know the Salesforce data model, Flow Builder basics (variables, elements, resources), and have a flow with input and output variables, as in the previous chapters. Error paths and run context get their own chapters, so they appear here only where they affect the logic.
What the logic has to do for an agent
A flow used by an agent differs from a screen flow or a record-triggered flow in one way. The caller is a language model that cannot see the flow, cannot click through a screen, and cannot recover from a half-finished transaction. The logic therefore follows three rules.
- Every path ends with the output variables set to something the agent can read.
- The flow does its work in one pass, with no screens and no waiting for a human.
- Data changes happen only after the flow has checked that they are safe.
A typical action follows the same four steps: read, decide, optionally write, and report.
Reading data with Get Records
Get Records finds records that match your filters. For an action, these settings matter most.
| Setting | Recommended choice | Reason |
|---|---|---|
| Object | The one object the action is about | Keeps the action single-purpose |
| Filter conditions | Compare a field to an input variable | The agent supplies the value at run time |
| How many records | Only the first record, when the filter targets a unique field | Gives you a single record variable instead of a collection |
| Fields to store | Only the fields you use | Smaller data footprint, and no unexpected field exposure |
| If no records are found | Leave the variable empty | Lets a decision element test for "not found" |
Here is the Get Records element from the case-status flow, as it appears in flow metadata.
<recordLookups>
<name>Get_Case</name>
<label>Get Case</label>
<locationX>176</locationX>
<locationY>134</locationY>
<assignNullValuesIfNoRecordsFound>false</assignNullValuesIfNoRecordsFound>
<connector>
<targetReference>Case_Found</targetReference>
</connector>
<faultConnector>
<targetReference>Set_Lookup_Error</targetReference>
</faultConnector>
<filterLogic>and</filterLogic>
<filters>
<field>CaseNumber</field>
<operator>EqualTo</operator>
<value>
<elementReference>caseNumber</elementReference>
</value>
</filters>
<getFirstRecordOnly>true</getFirstRecordOnly>
<object>Case</object>
<storeOutputAutomatically>true</storeOutputAutomatically>
</recordLookups>The filter compares the CaseNumber field to the caseNumber input variable. The faultConnector sends the flow to a fault path if the lookup itself fails, for example because of a permission problem. That is different from the lookup succeeding and finding nothing.
Matching what users actually type
The agent passes along whatever the customer said, after the model has interpreted it. A customer might say "case 1026", while the stored value is 00001026. An EqualTo filter on the raw input then finds nothing, and the agent tells the customer the case does not exist.
You have two options, and you can combine them.
- Tell the agent the exact format in the input description, for example "The Case Number, including leading zeros, such as 00001026."
- Normalize the value in the flow. A formula resource can pad the input with zeros before the lookup. One option is
LPAD({!caseNumber}, 8, "0").
The formula option is more reliable, because it does not depend on the model following the description. Add it for any identifier whose stored format differs from the way people say it.
Filter on more than the identifier
A case number alone lets any customer ask about any case. If the action should only return the case for the person in the conversation, add a second filter, such as ContactId equal to a contact ID the agent already has. Filtering in the flow is a design decision about what the action may return. The security chapter covers what the platform enforces underneath.
Deciding with Decision elements
After Get Records, the first decision is almost always whether a record came back. With a single-record result, test whether the record variable (or its Id) is null.
- Create a Decision element named
Case Found. - Add an outcome called
Foundwith the condition{!Get_Case}Is NullFalse. - Leave the default outcome for the not-found path, and label it
Not Found.
Naming the outcomes this way matters for people who maintain the flow. The agent never sees outcome names, but a future editor will.
Use further Decision elements for business rules that gate an update. Two examples are "only change a case that is not Closed" and "only apply a discount under a set limit." The flow is the right place for these rules. An instruction such as "never close an already-closed case" in the agent's prompt is a request the model may ignore, while a Decision element is a check that always runs.
Set the output on every branch
Each outcome must end by assigning values to the output variables. Use an Assignment element on each branch. A common pattern is a single output named resultMessage, plus any specific outputs the agent might quote.
Branch: Not Found
resultMessage = "No case with that number was found. Ask the customer to check the number."
Branch: Found
caseStatus = {!Get_Case.Status}
resultMessage = "Case " + {!Get_Case.CaseNumber} + " is " + {!Get_Case.Status} + "."Messages written for the agent can include a next step, such as "Ask the customer to check the number." The agent reads the output and usually acts on it.
Updating records safely
Flow offers several ways to change data. For actions, prefer the one that touches the fewest records.
| Approach | How it works | Use when |
|---|---|---|
| Update Records using the record variable from Get Records | Writes the fields you assigned back to that one record | The action changes a single, known record |
| Update Records with conditions | Finds records by criteria and sets fields on all matches | Rarely for actions, because one wrong filter changes many records |
| Create Records | Inserts a new record | The action logs a callback request, note, or task |
Follow this sequence for a single-record update.
- Run Get Records to load the record.
- Run a Decision that confirms the update is allowed.
- Use an Assignment to set the new field values on the record variable, for example
{!Get_Case.Priority}equals{!newPriority}. - Run Update Records with the record variable.
- Set the success output.
Gotchas for update logic
- A record-triggered flow may also run. If another flow or trigger fires on the same object, your action can set off extra changes. Read the other automation before you assume your flow is the only one.
- The agent can call an action twice. A customer may repeat a request, or the model may retry. Design updates so a second call is harmless. Setting a field to a value is safe to repeat. Adding to a counter is not.
- Do not put DML inside a loop. Create, Update, and Delete elements inside a Loop element run once per iteration, and Salesforce limits the number of data operations in one transaction. Build a collection in the loop and update it once after the loop ends.
- Do not update more than the customer asked for. If an action named "Update Case Priority" also reopens the case, an agent will trigger that by accident. Keep the writes in line with the action's name and description.
Confirming before changing data
For changes the customer should approve, the flow does not ask. Flow runs to completion in one call. The confirmation belongs to the agent's conversation, which you set up in the action instructions in a later chapter. A good design splits the work into two actions: one that looks up and returns the current value, and one that makes the change. The agent can then show the customer what will change, ask for approval, and only then call the update action.
Keeping the logic testable
Before you move on to errors and registration, check your flow against this list.
- Every outcome path reaches the end of the flow with all outputs set.
- Each Get Records element stores only the fields it needs.
- Updates sit behind a Decision element that checks the business rule.
- No element runs inside a loop that could be moved outside it.
- The flow runs from start to finish with no screen elements.
Exercise
A flow looks up a case by number, then updates its Priority to the value in an input variable. In testing, a customer gives a case number that does not exist, and the agent reports a confusing failure. Which two elements should you add or check, and where?
Show answer
Add a Decision element straight after Get Records that tests whether the record variable is null, so the not-found outcome is handled before any update. On that outcome, add an Assignment element that sets the output message (for example, "No case with that number was found. Ask the customer to check the number.") and skip Update Records. Also check that the Found outcome sets the outputs, so every path returns something the agent can read.
Exercise
An action must change the Status of all open cases for an account to "Escalated". Why is looping over the cases and calling Update Records inside the loop a poor design, and what is the better pattern?
Show answer
Update Records inside the loop runs once per iteration, which uses a data operation each time and can hit transaction limits as the number of cases grows. The better pattern is to loop over the cases only to set the Status field on each item and add it to a collection variable, then run a single Update Records element on the whole collection after the loop. Also consider whether a bulk change like this should be an agent action at all.