Advanced 15 min read

Governor Limits and Bulkification from LWC

By the end of this lesson, you'll be able to:

  • Recognize how LWC calling patterns can trigger Apex governor limits
  • Bulkify an Apex method call from a component
  • Avoid calling Apex inside a loop, once per record

Prerequisites: "Error Handling and Refreshing Apex-Backed Data"

Why This Matters From the Client Side

Governor limits (per-transaction caps on SOQL queries, DML statements, and callouts) are usually taught as a purely Apex concern — but a component's calling pattern can trigger them just as easily. Calling an Apex method once per row inside a loop creates one full transaction per call, each with its own limits, quickly compounding into real problems at scale.

The Anti-Pattern

// Avoid: one Apex call per row
for (const account of this.selectedAccounts) {
    await updateAccountStatus({ accountId: account.id, status: 'Active' });
}

Ten selected rows means ten separate Apex transactions — ten times the SOQL/DML overhead, and ten times slower for the user.

Bulkifying the Call Pattern

// Prefer: one Apex call for the whole batch
const accountIds = this.selectedAccounts.map(account => account.id);
await updateAccountStatuses({ accountIds, status: 'Active' });
@AuraEnabled
public static void updateAccountStatuses(List<Id> accountIds, String status) {
    List<Account> accounts = [SELECT Id, Status__c FROM Account WHERE Id IN :accountIds WITH SECURITY_ENFORCED];
    for (Account acc : accounts) {
        acc.Status__c = status;
    }
    update accounts;
}

One Apex call carrying a list of IDs, and one bulk update inside it — a single transaction handling every selected record, exactly the bulkification discipline Apex development already requires, applied at the calling boundary too.

Why This Matters in Real Projects

This is one of the most common real-world performance bugs in LWC/Apex integrations — a component that works fine in testing with 2-3 rows selected, then times out or hits limits in production when a user selects 200.

Exercise

Rewrite this loop that deletes each selected Contact one Apex call at a time into a single bulk call.

Show hint

Collect the IDs first, then make one call.

JAVASCRIPT

Exercise

Challenge: explain, as a comment, why "it works fine with a few test records" is not sufficient proof that a calling pattern is safe.

Show hint

Think about what changes as record counts grow.

JAVASCRIPT

Governor Limits and Bulkification from LWC Quiz

1. What problem can a component calling Apex once per row in a loop cause?

2. What is the fix for a one-call-per-row calling pattern?

3. Governor limits are exclusively an Apex-side concern, never affected by how a component calls Apex.

4. Why might an unbulkified calling pattern pass testing but fail in production?

5. In the bulk pattern example, what does the Apex method receive instead of a single Id?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A component that calls an Apex method once per row in a loop can trigger governor limits just as badly as poorly written Apex — the fix, bulkification, is a client-side discipline as much as a server-side one.