+ Start a Discussion
CezuCezu 

Problem with batch apex

I wrote this code : 

 

global class mailSendBatchJob implements Database.Batchable<sObject> {
public List<String> usr{get;set;} //
public List<sObject> usr_email{get;set;}
public Map<string,string> Map_usr{get;set;}

global mailSendBatchJob(){
}

global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([select id,E_mail__c from invoice_statement__c where Status__c = 'Otwarta']);
}

global void execute(Database.BatchableContext BC,List<sObject> scope){
usr=new List<String>();
usr_email=new List<sObject>();
usr.clear();
usr_email.clear();
usr_email=[select id,E_mail__c from invoice_statement__c where Status__c = 'Otwarta'];
Map_usr=new Map<string,string>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {};
for(sobject s : scope){
toAddresses.add(string.valueof(usr_email));
}

mail.setToAddresses(toAddresses);
mail.setSubject('Twoja faktura jest otwarta');
mail.setPlainTextBody('Twoja faktura jest otwarta');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

global void finish(Database.BatchableContext BC){

}
}

 

when i launch this batch apex give me error like this : 

 

Apex script unhandled exception by user/organization: 005i0000000Hm7C/00Di0000000HTKm

Failed to process batch for class 'mailSendBatchJob' for job id '707i00000008eVb'

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: LIMIT_EXCEEDED, Too many to addresses.: []

Class.mailSendBatchJob.execute: line 29, column 1

 

 

 

 

somebody show me what i'm doing wrong 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Then,I would suggest you to check debug logs and see No. of Email invocations column,ther you can see how many emails were invoked during the transaction.

All Answers

Vinit_KumarVinit_Kumar

Cezu,

 

You are hitting the governor limit for sending emails in a day.The limit for sending emails in a day is 1000. I think the list usr_email contains a large no. of email addresses.

 

 

 

 

 

CezuCezu

No in database i have only 183 e-mails :( and i only 2 times uses this batch

Vinit_KumarVinit_Kumar

Cezu,

 

Is there any other process which is sending emails ina day bacause the limit is 1000 emails per org in a day.

CezuCezu
no i have only one process which is sending mail
Vinit_KumarVinit_Kumar

Then,I would suggest you to check debug logs and see No. of Email invocations column,ther you can see how many emails were invoked during the transaction.

This was selected as the best answer
CezuCezu

I found :)

toAddresses -  An array of email address you are sending the email to. The maximum allowed is 100.

 

When i delete records with mail , apex job send mails :) 

THX

 

Vinit_KumarVinit_Kumar

Good to know :)