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
SalesForce 13SalesForce 13 

Even with batch it appears to me:" System.LimitException: Too many query rows: 50001"

I have a problem with the limit of queries in my trigger this error marks me: " System.LimitException: Too many query rows: 50001" when testing the test class, I already made a batch because according to that I could overcome the error that I sent when wanting to implement it in production, but keep sending me the error , any help or guide?

Trigger:
trigger TotalOficina on Account (after delete,after update, after insert, after undelete) {
List<String> listNumOficina = new List<String>();

	if(trigger.isInsert) {
		for(Account itemAccount : Trigger.New) {
			if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
				if (itemAccount.Suma_Total_Captaci_n__c != 0) {
				listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
				}
			}
		}
			if (listNumOficina.size() > 0) {
			Database.executeBatch(new cBatchTotal(listNumOficina));
			}
	}
		else if(trigger.isDelete) {
			for(Account itemAccount : Trigger.Old) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
					}
		}
		else if(trigger.isUnDelete) {
			for(Account itemAccount : Trigger.New) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
					}				
		}
		else if(trigger.isUpdate) {
		System.debug('CEAM CANTIDAD TRIGGER: ' + Trigger.New.size());
			for(Account itemAccount : Trigger.New) {
				if(itemAccount.Unidad_Ejecutora__c != null && itemAccount.Unidad_Ejecutora__c.trim() != '') {
					if (itemAccount.Suma_Total_Captaci_n__c != 0) {
					listNumOficina.add(itemAccount.Unidad_Ejecutora__c);
					}
				}
			}
					if (listNumOficina.size() > 0) {
					Database.executeBatch(new cBatchTotal(listNumOficina));
						}
		}
}

Batch
global class cBatchTotal implements Database.Batchable<sObject> {
	List<String> listNumOficina = new List<String>();
	Map<String, Oficina__c> oficinaMap = new Map<String, Oficina__c>();

	global cBatchTotal(List<String> listNumOficina) {
		this.listNumOficina = listNumOficina;
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		return DataBase.getQueryLocator([SELECT Id, UEJ__c FROM Oficina__c WHERE UEJ__c IN :this.listNumOficina]);
    }

    global void execute(Database.BatchableContext BC, List<Oficina__c> listOficina)
    {
		for(Oficina__c oOficina : listOficina) {
			oficinaMap.put(oOficina.UEJ__c, new Oficina__c(Id = oOficina.Id, UEJ__c = oOficina.UEJ__c, Total_Captacion_Oficina__c = 0));
		}

		for(Account cuenta : [SELECT Unidad_Ejecutora__c, Suma_Total_Captaci_n__c FROM Account WHERE Unidad_Ejecutora__c IN :this.listNumOficina]) {
			if (cuenta.Suma_Total_Captaci_n__c != null && cuenta.Suma_Total_Captaci_n__c != 0) {
				oficinaMap.get(cuenta.Unidad_Ejecutora__c).Total_Captacion_Oficina__c += cuenta.Suma_Total_Captaci_n__c;
			}
		}
		Database.update(oficinaMap.values());
    }

    global void finish(Database.BatchableContext BC) {

    }
}

And my @Test:
@isTest(SeeAllData=true)
private class TestctrlTotalOficina {
    Private static testMethod void insertAccountTest() {
        Test.startTest();
		//To Insert Account
		 
        Account oCuenta = new Account();
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		try {
			insert oCuenta;
		} catch(Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }

	Private static testMethod void updateAccountTest() {
        // To Update Account
		///////////////////////////////////////////
        List<Account> acctList = [SELECT Name, Unidad_Ejecutora__c FROM Account];
			//for(Account acct :acctList){
   			 //acct.Name = 'dasdds';
			//}
		update acctList;
///////////////////////////////////////////
        
        Account oCuenta = new Account();
		//oCuenta.Id = '0014P000029eezDQAQ';
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		insert oCuenta;
		oCuenta.Name = 'Otra Prueba';
      //  oCuenta.Suma_Total_Captaci_n__c = 0;
		Test.startTest();
        try {
			update oCuenta;
		} catch (Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }
    	Private static testMethod void deleteAccountTest() {
        // To Delete Account
		Account oCuenta = new Account();
        oCuenta.Name = 'Prueba Nombre';
        oCuenta.Unidad_Ejecutora__c = '205';
		insert oCuenta;
        oCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Id =:oCuenta.Id];
		Test.startTest();
        try {
			Delete oCuenta;
		} catch (Exception e) {
			e.getCause();
		}
		Test.stopTest();
    }

	Private static testMethod void undeleteAccountTest() {
		// To UnDelete Account
        Account oCuenta = new Account(Name = 'Prueba', Unidad_Ejecutora__c = '205');
		insert oCuenta;
		delete oCuenta;
		Account[] aCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Name = 'Prueba' ALL ROWS];
		try {
			undelete aCuenta;
		} catch (Exception e) {
			e.getCause();
		}
    }
}



 
Deepali KulshresthaDeepali Kulshrestha
Hi,
Greetings to you!

There is a limit to the number of records that can be retrieved by the SOQL Query which is 50000 records. Attempting to query the records having the number more than this will give the error which you are getting. To query the records more than its limit, you need to use a Batch Class which provides a fresh limit of 50k records by resetting the limits.


1) Replace row number 10 in batch class 
From this - 
return DataBase.getQueryLocator([SELECT Id, UEJ__c FROM Oficina__c WHERE UEJ__c IN :this.listNumOficina]);

To this - 
return DataBase.getQueryLocator([SELECT Id, UEJ__c FROM Oficina__c WHERE UEJ__c IN :this.listNumOficina LIMIT 50000]);

2) Replace row number 19 in batch class 
From this - 
for(Account cuenta : [SELECT Unidad_Ejecutora__c, Suma_Total_Captaci_n__c FROM Account WHERE Unidad_Ejecutora__c IN :this.listNumOficina]) {

To this - 
for(Account cuenta : [SELECT Unidad_Ejecutora__c, Suma_Total_Captaci_n__c FROM Account WHERE Unidad_Ejecutora__c IN :this.listNumOficina LIMIT 10000]) {

3) Replace row number 21 on Test class 
From this -  
List<Account> acctList = [SELECT Name, Unidad_Ejecutora__c FROM Account];

To this - 
List<Account> acctList = [SELECT Name, Unidad_Ejecutora__c FROM Account LIMIT 10000];


4) Replace row number 49 on Test class 
From this -
oCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Id =:oCuenta.Id];

To this - 
oCuenta = [SELECT Id, Name, Unidad_Ejecutora__c FROM Account WHERE Id =:oCuenta.Id LIMIT 10000];


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
SalesForce 13SalesForce 13
Hi Deepali
Thank you very much for answering me, apply the changes you recommended but now another error appears in another trigger that is not the one we are talking about in question, deactivate that trigger and the same error reappeared when testing the test class but now in the trigger that we modified (TotalOficina) and I get this "System.LimitException: Apex CPU time limit exceeded", do you think it can be fixed?

I hope you can help me and tell me a little more about your time
Regards!

User-added image