Understanding Trigger Execution for Upsert in Salesforce
Upsert is a combined DML operation in Salesforce that either inserts new records or updates existing ones based on an Id or an external ID field. A common interview question is: how many times does a trigger execute when you use upsert? The short, accurate answer is below, followed by details and examples.
Short Answer
For each record processed by an upsert, the trigger executes twice: once in the before context and once in the after context — but only for the operation that actually happens (insert or update). That means:
- If the record is inserted by the upsert, the trigger runs in
before insertandafter insert. - If the record is updated by the upsert, the trigger runs in
before updateandafter update.
Detailed Explanation
Upsert determines for each SObject whether it should be an insert or an update. It does not perform both for the same record. Triggers are fired based on the final action for that record. For bulk upserts containing a mix of new and existing records, the trigger will be invoked for both insert and update contexts within the same transaction — but only for the respective subsets of records.
Keep these key points in mind:
- Triggers have multiple entry points:
before insert,after insert,before update, andafter update. Each record that is inserted or updated will invoke the trigger in both the before and after phases for that operation. - For bulk operations (up to 200 records per batch), the trigger executes once per phase per batch, not once per record. However, logic inside the trigger may iterate over
Trigger.new, so from a per-record perspective, each record participates in both before and after events for its specific action. - Upsert does not cause a record to run as both insert and update. It is one or the other per record.
Example Trigger Snippet
trigger AccountUpsertTrigger on Account (before insert, after insert, before update, after update) {
if (Trigger.isBefore && Trigger.isInsert) {
// runs for inserted records by upsert
}
if (Trigger.isAfter && Trigger.isInsert) {
// runs after insert
}
if (Trigger.isBefore && Trigger.isUpdate) {
// runs for updated records by upsert
}
if (Trigger.isAfter && Trigger.isUpdate) {
// runs after update
}
}
Common Interview Pitfalls
Be careful with phrasing. People sometimes say “triggers execute twice on upsert” — that’s true only if you mean the before and after phases for the single action (insert or update). It’s incorrect to state that upsert causes both an insert and an update for the same record.
Summary
Per record: upsert triggers the record twice — once in the before phase and once in the after phase — but only for the action performed (insert or update). For bulk upserts with mixed records, triggers will execute for both insert and update contexts for their respective record sets within the same transaction.
Keywords: Salesforce upsert trigger, Apex trigger upsert, before insert, after insert, before update, after update, bulk upsert






Leave a Reply