You need to sign in to do that
Don't have an account?
Email Exception in batch apex
Hello
I have written the code below for case escalation i am getting an exception for it can someone please help me out
Thanks!
Code is :
global class CaseEscalation implements Database.Batchable<SObject>
{
global CaseEscalation()
{
}
global Database.QueryLocator start(Database.BatchableContext ctx)
{
return Database.getQueryLocator([SELECT CaseNumber, CreatedDate,Status FROM Case where CreatedDate<Today and Status!='Closed']);
}
global void execute(Database.BatchableContext ctx, List<Sobject> scope)
{
List<Case> ca = (List<Case>)scope;
//List <Case> ca = new List<Case>();
//for(SObject so :scope)
//{
// Case c = (Case)so;
// ca.add(c);
//}
System.debug('checking======='+ca);
for(Case c : ca)
{
// Double Days = Math.Floor((System.now().getTime() - c.CreatedDate.getTime())/ (1000.0*60.0*60.0*24));
//if(Days > 1 && c.Status != 'Closed' )
//{
System.Debug(c);
String emailMessage = 'The case ' + c.CaseNumber + ' has been ecalated ' + ' Please look into the case ' + 'Thankyou';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'reems.agarwal3@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Case Escalation');
mail.setPlainTextBody(emailMessage);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });
c.Status = 'Escalated';
// }
}
if(ca.size()>0)
update ca;
}
global void finish(Database.BatchableContext ctx)
{
}
}
I am getting the following exception:
Apex script unhandled exception by user/organization: 005U0000000DmS9/00DU0000000HTCU
Failed to process batch for class 'CaseEscalation' for job id '707U0000004bzCe'
caused by: System.LimitException: Too many Email Invocations: 11
Class.CaseEscalation.execute: line 35, column 1
When you are running the batch set the scope as follows:
Id batchId = database.executeBatch(new CaseEscalation, 10);
where 10 is your scope...it will be processing only 10 records in one batch and you wont be hitting that email limit.
Let me know if it works for you!
All Answers
Hi,
You can only invoke 10 Send Emails per Context. That is governor limit.
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_gov_limits.htm
To fix this you can set the scope of the batch so that it will process only limited records and you wont be hitting the limit for Email..
Let me know if it works for you!
how do i set these limits. could you please help me out with it
When you are running the batch set the scope as follows:
Id batchId = database.executeBatch(new CaseEscalation, 10);
where 10 is your scope...it will be processing only 10 records in one batch and you wont be hitting that email limit.
Let me know if it works for you!
I used that in my sheduled class
in my debug log i am getting
24.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
02:26:00.043 (43081000)|EXECUTION_STARTED
02:26:00.043 (43119000)|CODE_UNIT_STARTED|[EXTERNAL]|066U0000000KbJJ|VF: /apex/Cases
02:26:00.046 (46144000)|VF_APEX_CALL|j_id0|{!URLFOR($Action.Case.List, $ObjectType.Case)}|String:https://na12.salesforce.com/500/l?retURL=https%3A%2F%2Fc.na12.visual.force.com%2Fapex%2FCases%3Fsfdc.tabName%3D01rU00000000oCg%26core.apexpages.devmode.url%3D1
02:26:00.548 (49685000)|CUMULATIVE_LIMIT_USAGE
02:26:00.548|CUMULATIVE_LIMIT_USAGE_END
02:26:00.049 (49701000)|CODE_UNIT_FINISHED|VF: /apex/Cases
02:26:00.049 (49711000)|EXECUTION_FINISHED