Salesforce Decode
Salesforcedecode
Back to questions
ApexAdvancedconcurrencylockingmodstamp

Design Apex for optimistic locking with System Modstamp

Real World Scenario

Two reps edit same Opportunity offline mobile; last write wins silently drops first rep discount.

Expected Answer

• Client sends LastModifiedDate or custom version number on save • Trigger compares SystemModstamp rejecting stale updates with clear error • UI prompts refresh and merge on conflict • Optional custom Lock_Version__c incremented each save • Integration uses If-Unmodified-Since pattern in REST API • Audit conflicting updates for manager review • Test concurrent update simulation with parallel futures

Follow-Up Questions & Answers

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

Direct answer: Client sends LastModifiedDate or custom version number on save Also consider: Trigger compares SystemModstamp rejecting stale updates with clear error In practice: UI prompts refresh and merge on conflict Optimize for scale and operational observability.

Architect Perspective

Optimistic locking prevents silent data loss—essential for mobile and parallel integrations.