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
teeshuteeshu 

Too many query rows: 50001 in trigger handler class

I am a beginner to SF coding. I am getting this error  System.LimitException: Too many query rows: 50001 at line 17

my code is this, i am not sure why i am getting this error.

public class TransferTriggerHandler {
    public static final string COMMENT_TRUNCATED_MSG = Label.Comment_Truncated_Msg;
    public void OnAfterInsert(Transfer__c[] newTransObjects){
        
        
        List<Case> caseList = new List<Case>();
        List<CaseComment> caseCommentList = new List<CaseComment>();
        Map<id,Case> caseMap =  new Map<id,Case>([Select Id,Action_Owner__c,Action_Owner_Assignee__c,Minutes_Worked__c,accountId,account.ownerId from Case]); 
       
        boolean isPublish = false;
        String comment;
        
        for(Transfer__c trans: newTransObjects) {
            comment = 'New Transfer :'+trans.Name;
            comment += ' |Assigned Group:' + (trans.Action_Owner__c != null? trans.Action_Owner__c :' ') ;
            comment += ' |Summary of Issue:' + (trans.Summary_of_Issue__c != null ? trans.Summary_of_Issue__c : ' ');
            comment += ' |Customer Hardware Details:' + (trans.Customer_Hardware_Details__c != null ? trans.Customer_Hardware_Details__c : '') ;
            comment += ' |Work Done So Far:' + (trans.Work_Done_So_Far__c !=null ? trans.Work_Done_So_Far__c : '');
            comment += ' |Evidence/Supporting Details:' + (trans.Evidence_Supporting_Details__c !=null ? trans.Evidence_Supporting_Details__c : ''); 
            comment += ' |Expected Next Actions:' + (trans.Expected_next_actions__c != null ? trans.Expected_next_actions__c : '');
            comment += ' |Escalation Reason: ' + (trans.Escalation_Reason__c != null ? trans.Escalation_Reason__c : '');
            
            if (comment.length() > maxCommentLength) {
                //This comments is too long, we need to truncate it/
                comment =  comment.substring(0, maxCommentLength) + COMMENT_TRUNCATED_MSG;                    
            }
            
            Case c = caseMap.get(trans.case__c);
            CaseComment cc = new CaseComment(CommentBody= comment, IsPublished=isPublish,ParentId= c.id);
            caseCommentList.add(cc);
            insert caseCommentList;
           
            if(trans.Action_Owner__c =='Sales'){
           
                c.Action_Owner_Assignee__c = c.account.ownerid ;
                c.Action_Owner__c =trans.Action_Owner__c;
                c.Minutes_Worked__c= trans.Minutes_Worked__c;
            }
            else{
                c.Action_Owner_Assignee__c = null;
                c.Action_Owner__c =trans.Action_Owner__c;
                c.Minutes_Worked__c= trans.Minutes_Worked__c;
            }
            caseList.add(c);
        }
        try{
            if(caseList.size() > 0) {
                update caseList;
            }
        }  
        catch (Exception e){
            System.debug('Failed in transfer trigger handler class insertion'+e);
        }          
        
    }
    

    @TestVisible private static integer maxCommentLength {
        get
        {
            List<AddTransferFieldstoCase__c> tfc = AddTransferFieldstoCase__c.getall().values();
           
            integer returnValue = 4000;
           
            if (tfc.size() > 0) {
                returnValue = (integer)(tfc[0].MaxCommentLength__c);
            }

           
            returnValue = returnValue - COMMENT_TRUNCATED_MSG.length();

            if (returnValue < 0) throw new CommentConvertException('Maximum comment length is less than Zero. Check settings.');
            return returnValue;
        }
    }
    
    public class CommentConvertException extends Exception {}
}
Best Answer chosen by teeshu
Salesforce DeveloperSalesforce Developer
You are querying all the Cases in your org, which you don't need. As per your logic you only need cases which are referred in the Transfer__c records. So, query only these. Try as below:
public class TransferTriggerHandler {
    public static final string COMMENT_TRUNCATED_MSG = Label.Comment_Truncated_Msg;
    public void OnAfterInsert(Transfer__c[] newTransObjects){
        
        
        List<Case> caseList = new List<Case>();
        List<CaseComment> caseCommentList = new List<CaseComment>();
		Set<Id> caseIdSet = new Set<Id>();
		
		for(Transfer__c trans: newTransObjects) {
			if(trans.case__c != NULL)
				caseIdSet.add(trans.case__c);
		}
		
        Map<id,Case> caseMap =  new Map<id,Case>([Select Id, Action_Owner__c, Action_Owner_Assignee__c, Minutes_Worked__c,accountId,account.ownerId from Case where Id in: caseIdSet]); 
       
        boolean isPublish = false;
        String comment;
        
        for(Transfer__c trans: newTransObjects) {
            comment = 'New Transfer :'+trans.Name;
            comment += ' |Assigned Group:' + (trans.Action_Owner__c != null? trans.Action_Owner__c :' ') ;
            comment += ' |Summary of Issue:' + (trans.Summary_of_Issue__c != null ? trans.Summary_of_Issue__c : ' ');
            comment += ' |Customer Hardware Details:' + (trans.Customer_Hardware_Details__c != null ? trans.Customer_Hardware_Details__c : '') ;
            comment += ' |Work Done So Far:' + (trans.Work_Done_So_Far__c !=null ? trans.Work_Done_So_Far__c : '');
            comment += ' |Evidence/Supporting Details:' + (trans.Evidence_Supporting_Details__c !=null ? trans.Evidence_Supporting_Details__c : ''); 
            comment += ' |Expected Next Actions:' + (trans.Expected_next_actions__c != null ? trans.Expected_next_actions__c : '');
            comment += ' |Escalation Reason: ' + (trans.Escalation_Reason__c != null ? trans.Escalation_Reason__c : '');
            
            if (comment.length() > maxCommentLength) {
                //This comments is too long, we need to truncate it/
                comment =  comment.substring(0, maxCommentLength) + COMMENT_TRUNCATED_MSG;                    
            }
            
            Case c = caseMap.get(trans.case__c);
            CaseComment cc = new CaseComment(CommentBody= comment, IsPublished=isPublish,ParentId= c.id);
            caseCommentList.add(cc);
            insert caseCommentList;
           
            if(trans.Action_Owner__c =='Sales'){
           
                c.Action_Owner_Assignee__c = c.account.ownerid ;
                c.Action_Owner__c =trans.Action_Owner__c;
                c.Minutes_Worked__c= trans.Minutes_Worked__c;
            }
            else{
                c.Action_Owner_Assignee__c = null;
                c.Action_Owner__c =trans.Action_Owner__c;
                c.Minutes_Worked__c= trans.Minutes_Worked__c;
            }
            caseList.add(c);
        }
        try{
            if(caseList.size() > 0) {
                update caseList;
            }
        }  
        catch (Exception e){
            System.debug('Failed in transfer trigger handler class insertion'+e);
        }          
        
    }

 

All Answers

Nikhil Verma 6Nikhil Verma 6
Hi Sathish,

You are receiving this error since there is a governor limit of 50000 on the number of records returned from an SOQL. Right now you are querying all Cases in your org and adding it to caseMap. You need to add some filter criteria to limit your query results.

For more information on the Governor limits, please visit the below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

Hope this helps.
Salesforce DeveloperSalesforce Developer
You are querying all the Cases in your org, which you don't need. As per your logic you only need cases which are referred in the Transfer__c records. So, query only these. Try as below:
public class TransferTriggerHandler {
    public static final string COMMENT_TRUNCATED_MSG = Label.Comment_Truncated_Msg;
    public void OnAfterInsert(Transfer__c[] newTransObjects){
        
        
        List<Case> caseList = new List<Case>();
        List<CaseComment> caseCommentList = new List<CaseComment>();
		Set<Id> caseIdSet = new Set<Id>();
		
		for(Transfer__c trans: newTransObjects) {
			if(trans.case__c != NULL)
				caseIdSet.add(trans.case__c);
		}
		
        Map<id,Case> caseMap =  new Map<id,Case>([Select Id, Action_Owner__c, Action_Owner_Assignee__c, Minutes_Worked__c,accountId,account.ownerId from Case where Id in: caseIdSet]); 
       
        boolean isPublish = false;
        String comment;
        
        for(Transfer__c trans: newTransObjects) {
            comment = 'New Transfer :'+trans.Name;
            comment += ' |Assigned Group:' + (trans.Action_Owner__c != null? trans.Action_Owner__c :' ') ;
            comment += ' |Summary of Issue:' + (trans.Summary_of_Issue__c != null ? trans.Summary_of_Issue__c : ' ');
            comment += ' |Customer Hardware Details:' + (trans.Customer_Hardware_Details__c != null ? trans.Customer_Hardware_Details__c : '') ;
            comment += ' |Work Done So Far:' + (trans.Work_Done_So_Far__c !=null ? trans.Work_Done_So_Far__c : '');
            comment += ' |Evidence/Supporting Details:' + (trans.Evidence_Supporting_Details__c !=null ? trans.Evidence_Supporting_Details__c : ''); 
            comment += ' |Expected Next Actions:' + (trans.Expected_next_actions__c != null ? trans.Expected_next_actions__c : '');
            comment += ' |Escalation Reason: ' + (trans.Escalation_Reason__c != null ? trans.Escalation_Reason__c : '');
            
            if (comment.length() > maxCommentLength) {
                //This comments is too long, we need to truncate it/
                comment =  comment.substring(0, maxCommentLength) + COMMENT_TRUNCATED_MSG;                    
            }
            
            Case c = caseMap.get(trans.case__c);
            CaseComment cc = new CaseComment(CommentBody= comment, IsPublished=isPublish,ParentId= c.id);
            caseCommentList.add(cc);
            insert caseCommentList;
           
            if(trans.Action_Owner__c =='Sales'){
           
                c.Action_Owner_Assignee__c = c.account.ownerid ;
                c.Action_Owner__c =trans.Action_Owner__c;
                c.Minutes_Worked__c= trans.Minutes_Worked__c;
            }
            else{
                c.Action_Owner_Assignee__c = null;
                c.Action_Owner__c =trans.Action_Owner__c;
                c.Minutes_Worked__c= trans.Minutes_Worked__c;
            }
            caseList.add(c);
        }
        try{
            if(caseList.size() > 0) {
                update caseList;
            }
        }  
        catch (Exception e){
            System.debug('Failed in transfer trigger handler class insertion'+e);
        }          
        
    }

 
This was selected as the best answer