Sub topic 4
Data and logic elements you will use most
This chapter covers the elements you will place in almost every scheduled or autolaunched flow. It assumes you know the Salesforce data model (objects, fields, records) and can open Flow Builder. It also assumes you have met variables, collections, and formulas, which the previous chapter covered.
Scheduled and autolaunched flows have no screen and no user waiting on them. Every decision and every data change has to be explicit. The six elements below do most of that work.
The elements at a glance
| Element | Purpose | Uses a DML or query operation? |
|---|---|---|
| Get Records | Reads records from the database | Yes, one query |
| Create Records | Inserts new records | Yes, one DML statement |
| Update Records | Changes existing records | Yes, one DML statement |
| Delete Records | Removes records | Yes, one DML statement |
| Decision | Branches the path based on conditions | No |
| Assignment | Sets variable values and adds items to collections | No |
| Loop | Visits each item in a collection | No, but anything inside it may be |
DML means Data Manipulation Language. It is the family of operations (insert, update, delete) that change data. Salesforce limits how many of these a single transaction can run, so the last column matters.
Get Records
Get Records runs a query and stores the result. You choose an object, add filter conditions, set a sort order, and decide what to store.
- Only the first record stores one record in a record variable. Use it when you expect a single match, such as the parent Account.
- All records stores a collection. Use it when you want to process many records.
You also choose which fields to store. The safest choice is Choose fields and let Salesforce do the rest, which stores only the fields the flow references. Storing all fields works but is slower and uses more heap memory.
If a query finds nothing, a single-record variable stays empty and a collection stays empty. The flow does not fail. A later element that reads a field from the empty record variable gives you a null value, which is a common source of silent bugs. Check for this with a Decision.
Create, Update, and Delete Records
Each of these has two modes, and the choice affects how many database operations you spend.
Update Records
- Specify conditions and set fields finds records by filter and sets the same field values on all of them. It queries and updates in one element, so it is convenient but updates the same value everywhere.
- Use the IDs and all field values from a record or collection takes records you already built or changed in the flow and writes them back. This is the right mode when each record needs different values.
Create Records can take one record variable or a collection. Passing a collection creates all of the records in a single DML statement.
Delete Records accepts a record, a collection, or filter conditions. Deletes are hard to undo, so test with a narrow filter first.
The rule to remember is that one Update, Create, or Delete element costs one DML statement no matter how many records it handles. That fact drives the bulk-safe pattern described later in this chapter.
Decision
A Decision element evaluates outcomes in order from top to bottom. The first outcome whose conditions are true is taken. If none match, the flow follows the Default Outcome.
Things to keep in mind:
- Order matters. Put the most specific outcome first.
- Each outcome has its own condition logic: All conditions are met (AND), Any condition is met (OR), or a custom logic string such as
1 AND (2 OR 3). - Compare against null with the operator Is Null set to True, not by testing for an empty string.
- Always label the Default Outcome clearly. In unattended flows it is where unexpected data ends up.
Assignment
Assignment changes variables. You pick a variable, an operator, and a value. Common operators are Equals, Add (to a collection or a number), and Add Item for collections.
Assignment does not touch the database. Changing a field on a record variable only changes the in-memory copy until an Update Records element saves it.
Loop
A Loop visits each item of a collection, one at a time. It has two paths:
- For Each Item runs once per item, and the current item is available as a loop variable.
- After Last Item runs once when the loop finishes.
The bulk-safe pattern
The most important idea in this chapter is to keep database elements out of loops. Instead, use the Loop and an Assignment to prepare records in memory, then save them once after the loop.
In this design the flow runs one query and one DML statement however many cases it finds. If you placed the Update Records element inside the loop, the flow would spend one DML statement per record. Salesforce allows 150 DML statements per transaction, so the flow would fail on the 151st record.
The same applies to Get Records inside a loop. The limit there is 100 SOQL queries per transaction.
Worked example
Suppose an autolaunched flow named Close_Stale_Cases receives an accountId and closes that account's old open cases. Its structure looks like this:
- Get Records on Case, filtered by
AccountIdequalsaccountId,IsClosedequals False, andLastModifiedDateless than a formula resourcecutoffDate. Store all records instaleCases. - Decision that checks whether
staleCasesis empty. Use a formula resourcehasStaleCaseswith the expressionNOT(ISEMPTY({!staleCases})). If false, go to the end. - Loop over
staleCases. - Assignment inside the loop, which sets
{!currentCase.Status}EqualsClosed, then adds{!currentCase}to a collection variablecasesToUpdate. Also adds 1 to a number variableclosedCount. - Update Records after the last item, using the IDs and all field values from
casesToUpdate.
The formula for the cutoff is:
TODAY() - 30Here closedCount can be an output variable so callers can read how many cases were closed. The next chapters show how.
Common errors
- Update inside a loop. Causes "Too many DML statements: 151". Move the update after the loop.
- Reading a field from an empty record variable. Gives null and may produce a bad result later. Add a Decision after Get Records.
- Forgetting that an Assignment is in memory only. Records change in the flow but not in the database until an Update Records element runs.
- Updating the loop variable directly. Changes made to the loop variable inside the loop do not change the collection. Copy the changed item into a separate collection, as in the example.
- Wrong Decision order. A broad outcome placed first hides narrower ones below it.
- Filtering on a formula that returns the wrong type. A date field needs a date value, not text.
Exercise
A flow loops through 400 Opportunity records. Inside the loop, an Assignment sets the Stage field, and an Update Records element then saves the single current record. What goes wrong, and how do you fix it?
Show answer
The Update Records element runs once per record, so the flow uses one DML statement per iteration. After 150 statements the transaction fails with a DML limit error. Fix it by keeping the Assignment inside the loop but also adding each changed record to a collection variable. Then put one Update Records element after the loop, using the IDs and all field values from that collection. The flow then uses a single DML statement for all 400 records.
Exercise
A Get Records element that stores only the first record finds no match. What does the flow do, and how should you guard against it?
Show answer
The flow does not fail. The record variable stays empty, so any later reference to its fields returns null. Add a Decision after Get Records that checks whether the record variable's Id Is Null, and route the null case to a safe path, such as ending the flow or logging the problem.