Incremental filter uses LastModifiedDate instead of SystemModstamp

ID

apex.performance.soql_systemmodstamp

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Performance

Language

Apex

Description

Reports LastModifiedDate used in a SOQL WHERE or ORDER BY. Selecting the field for display is not reported.

Rationale

SystemModstamp is indexed and LastModifiedDate is not, so an incremental sync filtering on the latter scans the object instead of seeking, and the platform will refuse the query as non-selective once the object is large. The two fields also differ in meaning: LastModifiedDate changes only when a user or Apex writes the record, while SystemModstamp also moves when the platform itself touches it - so a sync keyed on LastModifiedDate silently misses system-driven updates and leaves the two systems quietly out of step.

The following code illustrates the pattern detected by this rule:

public class IncrementalSync {

    public List<Account> changedSince(Datetime since) {
        // FLAGGED: Incremental filter uses LastModifiedDate instead of SystemModstamp
        return [SELECT Id, Name FROM Account WHERE LastModifiedDate > :since];
    }

    public List<Contact> newestFirst() {
        // FLAGGED: Incremental filter uses LastModifiedDate instead of SystemModstamp
        return [SELECT Id FROM Contact WHERE CreatedDate = THIS_MONTH ORDER BY LastModifiedDate DESC];
    }

    public List<Account> changedSinceStamp(Datetime since) {
        return [SELECT Id, Name FROM Account WHERE SystemModstamp > :since];
    }

    public List<Account> auditTrail(Id accountId) {
        return [SELECT Id, LastModifiedDate FROM Account WHERE Id = :accountId];
    }
}

Remediation

Filter and order on SystemModstamp, keeping LastModifiedDate in the SELECT list where the user needs to see who changed the record and when. Store the high-water mark from the same field you filter on, or the next run re-reads or skips a window.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved