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
Paras JainParas Jain 

To much aggregate Query

SELECT Id, AccountId, (SELECT Id,Core__c FROM Properties__r where Core__c !=null ) FROM Contact WHERE Accountid IN : accountIds

This is my Query..Any limit i have to put in inner query....

This is my trigger.......
trigger updateAccCorefrmProperty on Property__c (after insert, after update, after delete) {


      Set<Id> accountIds = new Set<Id> ();                   
      Map<id,List<Property__c>> mpAccProperty = new  Map<id,List<Property__c>>();     
      Set<String>  PropCores= new Set<String>();
      List<Account> accountListToUpdate = new List<Account>();
      List<Account> accountListToUpdate2 = new List<Account>();
      set<id>  contactId= new set<id> ();
      set<id>  accountwithoutProp = new set<id> ();
                
        if(Trigger.IsInsert ) {
            for(Property__c prp: [SELECT Contact__c, core__c, Contact__r.Accountid from Property__c WHERE Id IN : Trigger.new] )
            {
                if(prp.Contact__c!=null && prp.core__c !=null && prp.Contact__r.Accountid !=null)
                accountIds.add(prp.Contact__r.Accountid);
            }
        
        }
        
        if( Trigger.IsUpdate ) {
            for(Property__c prp: Trigger.new )
            {
               if(prp.Contact__c!=null && prp.core__c !=null)
                contactId.add(prp.Contact__c); 
            }
            
            for(Property__c prp: Trigger.old)
            {
               if(prp.Contact__c!=null && prp.core__c !=null)
                contactId.add(prp.Contact__c); 
            }
            
             for(Contact con: [select id, AccountId from Contact where id in : contactId and AccountId != null])
                accountIds.add(con.Accountid);
       
        }
        
        
        
        if(Trigger.IsDelete ){
            for(Property__c prp :Trigger.old){
                if(prp.Contact__c!=null && prp.core__c !=null)
                contactId.add(prp.Contact__c);        
            }

            for(Contact con: [select id, AccountId from Contact where id in : contactId and AccountId != null])
            accountIds.add(con.Accountid);
        }    
   
        for(Contact conT: [SELECT Id, AccountId, (SELECT Id,Core__c FROM Properties__r where Core__c !=null limit 2000) FROM Contact WHERE Accountid IN : accountIds]){
            if(conT.Properties__r != null && conT.Properties__r.Size() > 0) {
                if(mpAccProperty.containskey(cont.AccountId)){
                List<Property__c> lstProperty= mpAccProperty.get(cont.AccountId);
                lstProperty.addall(conT.Properties__r);
                }
                else{
                mpAccProperty.put(cont.AccountId,conT.Properties__r);}  
            }
            system.debug('wwwwwwwconT.Properties__r.Size()www'+conT.Properties__r.Size());
            if( conT.Properties__r.Size() == 0) {  
                accountwithoutProp.add(cont.AccountId);
                system.debug('wwwwwwwwww'+accountwithoutProp);
            } 
        }
        
        for(Id accId: mpAccProperty.keyset()){
            if(accountwithoutProp.contains(accId))
                accountwithoutProp.remove(accid);
        }
        
           
        String new_propCore;
        for(Id accId: mpAccProperty.keyset()){
            new_propCore='';
            PropCores.clear();
            for(Property__c prop: mpAccProperty.get(accId))
            PropCores.add(prop.core__c);

            new_propCore=String.Join(new List<String>(PropCores),',');
            
            if(!String.IsBlank(new_propCore))
            accountListToUpdate.add(new Account(Id=accId,core__c=new_propCore));         

        }
        if(accountwithoutProp!=null && accountwithoutProp.size()>0){
            for(Id accwithoutPropId : accountwithoutProp){
                accountListToUpdate.add(new Account(Id=accwithoutPropId,core__c=''));
            }
        }
        
        
        //if(accountListToUpdate2.size()>0)
        //update accountListToUpdate2;

        if(accountListToUpdate.size()>0)
        update accountListToUpdate;//update Impacted account 
        
        
}



>>>>>>>>>>>>>

ny using batch m updating Properties...and this trigger fires
Chamil MadusankaChamil Madusanka
Explain your question clearly. 
Paras JainParas Jain
I am getting error ... Aggregate query has too many rows 
Paras JainParas Jain
can u pls tell is there anyproblem in query...this error comes for those contacts having properties around 1000..
 
Nishant SharmaNishant Sharma
I can't see any aggregate query in your code above. However, such problem comes when your query returns more then 50k records. In case of Aggregate query, this limit is not about how many number of result aggregate query returns, it is about how many record aggregate query has touched for this aggregation.

May be you can put explicit limit of 50k, OR another solution is to have batch apex, where you can process bulk of records.

Hope this helps.