Salesforce Decode
Salesforcedecode
Back to questions
ApexAdvancedperformancetriggerslazy-load

Optimize trigger performance with lazy loading patterns

Real World Scenario

Before update trigger on Opportunity queries 8 related objects even when only Stage changed.

Expected Answer

• Detect changed fields via Trigger.oldMap comparison • Lazy load related data only when dependent fields changed • Early return for integration bypass scenarios • Consolidate related queries into single query per object type • Cache query results in static map for recursion within transaction • Profile CPU time before and after optimization

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Direct answer: Detect changed fields via Trigger.oldMap comparison Also consider: Lazy load related data only when dependent fields changed In practice: Early return for integration bypass scenarios Optimize for scale and operational observability.

Architect Perspective

Conditional execution is bulkification companion—skip work when inputs unchanged.