function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saravana RavikumarSaravana Ravikumar 

If there are over 10000 account records , Will the transaction succeed and changes be commited ?

List<Account> accountstoUpdate = new List<Account>();
For(Account thisAccount : [Select id, Status FROM Account LIMIT 50000]){
ThisAccount.Status = ‘Active’;
AccountsToUpdate.add(thisAccount);
}
try{
Database.update(AccountstoUpdate,false);
}catch (Exception e){
System.debug(e.getMessage());
}
Best Answer chosen by Saravana Ravikumar
Arun Kumar 1141Arun Kumar 1141

Hi Saravana,

The code snippet you provided attempts to update a list of Account records. However, there are a few considerations regarding the Salesforce governor limits that you should be aware of:

1. Query Limit: In a single transaction, you can query up to 50,000 records. In your code, you are querying 50,000 Account records using a SOQL query with a LIMIT of 50,000. If the total number of Account records in your org exceeds 50,000, this query will exceed the limit and result in a runtime exception.

2. DML Statement Limit: In a single transaction, you can perform DML (Data Manipulation Language) operations, such as inserts, updates, or deletes, on up to 10,000 records. In your code, you are attempting to update the list of Account records, which may include more than 10,000 records. If the total number of records to be updated exceeds 10,000, the update operation will fail and throw a runtime exception.

To handle scenarios where you have a large number of records to update, you can leverage Salesforce's batch apex framework. Batch apex allows you to process large data sets in smaller chunks, adhering to the governor limits.

Here's an example of how you can modify your code to use batch apex:
 
public class UpdateAccountStatusBatch implements Database.Batchable<Account> {
    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator([SELECT Id, Status FROM Account]);
    }

    public void execute(Database.BatchableContext context, List<Account> scope) {
        for (Account acc : scope) {
            acc.Status = 'Active';
        }
        update scope;
    }

    public void finish(Database.BatchableContext context) {
        // Perform any post-processing operations if needed
    }
}

Trigger the batch job:
UpdateAccountStatusBatch batchJob = new UpdateAccountStatusBatch();
Database.executeBatch(batchJob, 200); // Specify the batch size (e.g., 200)

In this example, the UpdateAccountStatusBatch class implements the Database.Batchable interface, allowing you to process the Account records in smaller batches. The start method defines the initial query locator, the execute method processes each batch of records, and the finish method handles any post-processing tasks.

By using batch apex, you can efficiently update a large number of records while staying within the Salesforce governor limits.

Hope this will be helpful.
Thanks!