Sub topic 2
Configuring the Object and Trigger Event
This chapter covers the first screen you see when you build a record-triggered flow: the object and the trigger event. These two choices decide when your flow starts and which record it receives. They also limit which optimizations you can use later.
It assumes you know the basic Salesforce data model, can place the main Flow Builder elements, and understand that a database transaction either commits or rolls back as a whole.
What the Start element decides
Every record-triggered flow begins with a Start element. In the Start element you set three things in order:
- The object. This is the one object whose records can start the flow.
- The trigger event. This is the kind of change on that object that starts the flow.
- The optimization. This chooses before-save (Fast Field Updates) or after-save (Actions and Related Records).
Entry conditions and run frequency come after these three, and the next chapter covers them. Here we focus on the first two choices and on how they affect the third.
Choosing the object
A record-triggered flow listens to exactly one object. If you need the same logic on Account and Contact, you build two flows. You cannot pick several objects in one Start element.
Some practical points about object selection:
- The object can be a standard object, such as Opportunity, or a custom object, such as
Invoice__c. - Not every object supports record-triggered flows. If an object does not appear in the picker, it does not support them.
- The object is fixed once you save the flow. If you picked the wrong object, you normally create a new flow instead of changing it.
- The flow receives the triggering record in the
{!$Record}variable. Its fields are the fields of the object you chose, so the object choice also decides which fields you can reference.
Choosing the trigger event
The trigger event says which database operation starts the flow. Flow Builder offers these options:
| Trigger option | When it starts | Optimizations available |
|---|---|---|
| A record is created | After a new record is inserted | Fast Field Updates, Actions and Related Records |
| A record is updated | After an existing record is changed | Fast Field Updates, Actions and Related Records |
| A record is created or updated | On either operation | Fast Field Updates, Actions and Related Records |
| A record is deleted | When a record is deleted | Actions and Related Records only (before-delete and after-delete both run in this mode) |
A few details deserve attention.
Create, update, and create or update
The "created or updated" option is a single flow that covers both operations. It is convenient, but it means your logic must work for both cases. On a create, there is no previous version of the record. On an update, there is one. The next chapters show how {!$Record__Prior} behaves in each case. For now, remember that {!$Record__Prior} has no values on a create, so a condition that depends on it needs care.
Choose "created" or "updated" alone when the requirement is specific to one of them. A narrower trigger gives you a flow that is easier to read and less likely to run when you did not intend it.
Delete
A flow that runs on delete cannot change the record being deleted, because that record is on its way out. This is why before-save optimization is not offered for delete. In a delete flow, {!$Record} holds the values of the record as it was. You typically use it to act on related records, for example to log the deletion or to update a parent.
What counts as an update
An update is any save of an existing record, even if the user changed nothing visible. A save that triggers a flow includes edits from the UI, API calls, data loads, Apex DML, and changes made by other automation. Salesforce does not ask which tool made the change. This is why flows can fire in places you did not expect, and why entry conditions matter so much.
Two operations that you may expect to count are handled differently:
- Undelete (restoring from the Recycle Bin) is not one of the trigger options in the Start element.
- Merge is a special case. The losing records are deleted and the winning record is updated, so related flows can run for those events.
How the trigger event limits the optimization
The trigger event and the optimization are linked. The optimization tells Salesforce whether the flow runs before or after the record is written to the database (but before the transaction commits).
- A before-save flow (Fast Field Updates) runs before the record is saved. It can only change fields on the triggering record, using the record variable. It is available for create, update, and create or update.
- An after-save flow (Actions and Related Records) runs after the record is saved. It can create and update other records, call actions, and run asynchronous paths. It is available for all trigger events.
Here is an example of a Start element for a before-save flow, as shown in the topic's sample configuration:
Flow: Opportunity - Set Next Step (before-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Fast Field Updates
Entry condition: {!$Record.Amount} > 100000
Run: Only when a record is updated to meet the condition requirements
Assignment element
Variable: {!$Record.NextStep}
Operator: Equals
Value: Review large dealThe object is Opportunity, the trigger event covers both create and update, and the optimization is Fast Field Updates. Because it is before-save, the Assignment element writes straight to {!$Record.NextStep}. The record has not yet been saved, so no Update Records element is needed.
Compare the after-save example, which creates a related Task:
Flow: Opportunity - Create Follow-up Task (after-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Actions and Related Records
Entry condition: {!$Record.StageName} = "Closed Won"
Run: Only when a record is updated to meet the condition requirements
Create Records element (object: Task)
Subject: Send welcome package
WhatId: {!$Record.Id}
OwnerId: {!$Record.OwnerId}Both flows use the same object and the same trigger event. Only the optimization differs, and that choice changes what the flow can do. The after-save flow can use {!$Record.Id} because the record has been saved and has an Id. In a before-save flow on a create, the Id does not exist yet.
Common configuration mistakes
These errors come up often when people first set the object and trigger event.
- Picking "created or updated" by default. The flow then runs more often than needed. If the requirement says "when a new Lead arrives," choose "created."
- Expecting a before-save flow to affect other records. Before-save flows can change only the triggering record. If you need to touch a child or parent record, you need after-save.
- Reading
{!$Record.Id}in a before-save create flow. The Id is empty because the record has not been inserted yet. - Forgetting that other tools save records. A data load or an integration also fires the flow. Test with the paths that real users and systems use, not only the UI.
- Using a delete trigger to block a delete by changing the record. The flow cannot change the record. If you must stop a delete, use a different mechanism. A custom error in an after-save flow can roll back the transaction, and a later chapter covers that.
Verifying the configuration
You can confirm that a flow starts on the right event by writing a small Apex test. This test assumes the before-save flow shown above is active. It inserts a large Opportunity and checks that the flow set NextStep.
@IsTest
private class OpportunityFlowTest {
@IsTest
static void beforeSaveFlowSetsNextStep() {
Opportunity opp = new Opportunity(
Name = 'Large deal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 200000
);
insert opp;
Opportunity saved = [SELECT NextStep FROM Opportunity WHERE Id = :opp.Id];
Assert.areEqual('Review large deal', saved.NextStep);
}
}The test uses insert, so it exercises the create path of the "created or updated" trigger. To cover the update path, change the record and call update. Because the flow runs inside the same transaction as the DML, the test can read the result right after the statement.
Quick reference
- One flow, one object.
- The trigger event chooses create, update, create or update, or delete.
- Delete flows use the after-save style only.
- The optimization decides whether you edit the record in place (before-save) or work with other records and actions (after-save).
- Any save from any source counts, so narrow the trigger and add entry conditions.
Exercise
A requirement says: "When an Opportunity is created, set its NextStep to a default text." Which object, trigger event, and optimization would you choose, and why would you avoid "A record is created or updated"?
Show answer
Choose the Opportunity object, the trigger "A record is created", and the optimization "Fast Field Updates" (before-save). The change affects only the triggering record, so a before-save flow works without extra DML. The narrower "created" trigger keeps the flow from running on every later edit, where it could overwrite a NextStep that a user entered.
Exercise
You build a flow on Account that must create a Task related to the Account whenever an Account is created. Why can't you build it as a before-save flow, and what happens to {!$Record.Id} in each case?
Show answer
A before-save flow can change only the triggering record, so it cannot create a Task. You need after-save (Actions and Related Records). In a before-save create flow, {!$Record.Id} is empty because the record is not yet inserted. In an after-save flow, the record is saved and has an Id, so you can use it as the Task's WhatId.