An after-update Opportunity trigger writes to related records, and that write re-fires the same trigger inside the same transaction. Without a re-entrancy guard the same record is handled several times: duplicate notifications, doubled rollups, and SOQL and DML limits burned on work that was already done. The fix is a static Set of Id named processedOpportunityIds, declared above the method, because a static variable is initialized once per transaction and keeps its value across every invocation inside that transaction, which is exactly the lifetime a recursion guard needs.
Write a method that processes a list of Opportunities exactly once per transaction. If opps is null or empty, return an empty List<String> immediately. Otherwise walk opps in order. For each record, if its Id is not null and processedOpportunityIds already contains that Id, skip it. Otherwise append its Name to the result, and if its Id is not null, add that Id to processedOpportunityIds. A record with a null Id was never saved and cannot be identified, so it is always processed and is never added to the guard, because a Set of Id can hold null and contains(null) would return true afterward, making every later unsaved record look like it was already handled. Names are returned in input order.
Examples:
Input: opps = [Opp(Id=006000000000101AAA, Name=Alpha Deal), Opp(Id=006000000000102AAA, Name=Beta Deal)]
Output: [Alpha Deal, Beta Deal]
Explanation: Neither Id has been seen before, so both records are processed, both Names are returned in input order, and both Ids are added to the guard
Input: the same two records passed to the method again in the same transaction
Output: []
Explanation: Both Ids are already in processedOpportunityIds, so every record is skipped and an empty list is returned
Input: opps = null
Output: []
Explanation: A null input list is guarded and returns an empty list immediately
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
Contest runs September 1 - 15. Complete challenges to climb the leaderboard!
1st & 2nd: Salesforce certification exam voucher — see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.