0

I am trying to run an aggregate SOQL query on Account object but i am getting too many rows error even though it should have returned only one row.

public static void updateAUMForAccounts(List<Id> accountIds) {

    Map <Id, Account> accountMap = new Map<Id, Account>([select Id, Total_DB_AUM__c, Total_DC_AUM__c, Total_EF_AUM__c, Total_HSA_AUM__c, Total_NQD_AUM__c from Account where Id in :AccountIds]);
    List<AggregateResult> results = [select AccountId, SUM(DB_AUM__c) Total_DB_AUM__c, SUM(DC_AUM__c) Total_DC_AUM__c, SUM(EF_AUM__c) Total_EF_AUM__c, SUM(HSA_AUM__c) Total_HSA_AUM__c, SUM(NQD_AUM__c) Total_NQD_AUM__c from Contact where AccountId in :AccountIds group by AccountId];

    // Do something with result
}

this is called by my trigger on contact with the list of all distinct accountIds, but even when i call it with only one Id in the list it gives me System.LimitException: Too many query rows: 50001. The error is on the second query.

Trigger code

trigger accountAUMCalculation on Contact (after update) {
    Set<Id> accountIds = new Set<Id>();
    for (Contact contact: trigger.new) {
        accountIds.add(contact.AccountId);        
    }
    accountAUMHandler.updateAUMForAccounts(new List<Id>(accountIds));
}

Called the method from anonymous window

List<Id> ids = new List<Id>();
ids.add(Id.valueOf('0013000000cfleIAAQ'));
accountAUMHandler.updateAUMForAccounts(ids);

Update

This was because of a stupid mistake that i made, the var passed in is accountIds and the var used in queries is AccountIds

3
  • can you add trigger code Jan 25, 2017 at 19:46
  • Make sure your filter collection does not contain null.
    – Adrian Larson
    Jan 25, 2017 at 19:47
  • i added the Trigger code, but it shouldn't have nulls in List Jan 25, 2017 at 19:49

1 Answer 1

0

You can loop query inside For loop. This expands the ceiling beyond the 50,000 limit.

for (AggregateResult result : [
    SELECT AccountId, SUM(DB_AUM__c) Total_DB_AUM__c, SUM(DC_AUM__c) Total_DC_AUM__c, SUM(EF_AUM__c) Total_EF_AUM__c, SUM(HSA_AUM__c) Total_HSA_AUM__c, SUM(NQD_AUM__c) Total_NQD_AUM__c 
                                        from Contact where 
                                        AccountId 
                                        in :AccountIds group by AccountId
]) 
3
  • this solves the issue but i am still curious why it was happening in first place. For the limit, does it look at number of records it's querying over or number of records in final result ? Jan 25, 2017 at 20:32
  • can you put a debug log for each query and check how many records are returning? Jan 25, 2017 at 20:33
  • in the logs for both queries it actually says 1 row Jan 25, 2017 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .