You need to sign in to do that
Don't have an account?
teeshu
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 {}
}
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 {}
}
All Answers
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.