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
AbAb 

System.LimitException: Too many query rows: 50001 when trying to get the latest account inserted

Hello,

I have below code which was workign in Sandbox but on production it fails because records are more than 50000, how can i solve this issue without regression.
public static void PutCodeForAccount( List<Account> accountList ){
Account acc = getLatestAccount();
if(acc != null) {
            Integer intValue = getIntFromName(acc.Code__c);
}

//Method to get the Last inserted Record based on created Date for the existing records
    private static Account getLatestAccount() {
        List<Account> accounts = [SELECT ID, NAME, Code__c  FROM Account ORDER BY CreatedDate Desc];  ///// System.LimitException: Too many query rows: 50001
        if(accounts.size() > 0) {
            return accounts[0];
        }
        return null;
    }

// Method to spilt the first 4 characters from concatinated number
    private static Integer getIntFromName(String Name) {
        String phrase = Name;
        
        Integer rephrase = null;
        
        if(phrase != null && phrase.length() > 4) {
            phrase = phrase.substring(4, phrase.length());
            if(phrase.isNumeric()) {
                rephrase = Integer.valueOf(phrase);
            }
        }
        return rephrase;
    }

The account code(Code__c) lookss like ABCD980001

So the above code takes the integer value of the code from the latest inserted account.

I just want to get the last inserted account

Thanks for help
Best Answer chosen by Ab
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

If you just want the last inserted record, you can use LIMIT 1 in SOQL as suggested by Pradeep.

Total number of records retrieved by SOQL queries = 50,000. You will need to use Batch Apex, in which the 50k limit counts per batch execution.

These limits count for each Apex transaction. For Batch Apex, these limits are reset for each execution of a batch of records in the execute method.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch.htm?search_text=batch

https://webkul.com/blog/batch-apex/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Pradeep LandgePradeep Landge
Rewrite SOQL with limit
SELECT ID, NAME, Code__c  FROM Account ORDER BY CreatedDate Desc LIMIT 1

If this helps you please mark it as solved.
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

If you just want the last inserted record, you can use LIMIT 1 in SOQL as suggested by Pradeep.

Total number of records retrieved by SOQL queries = 50,000. You will need to use Batch Apex, in which the 50k limit counts per batch execution.

These limits count for each Apex transaction. For Batch Apex, these limits are reset for each execution of a batch of records in the execute method.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch.htm?search_text=batch

https://webkul.com/blog/batch-apex/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer