1

I have written the following code. On execution, it is not throwing any error. But I am not getting any mail, or any log statements.

public class BatchApexBL implements Database.batchable<Sobject>,Database.Stateful{

    public integer count = 0;

    public Database.QueryLocator start(Database.BatchableContext bc){

        String str ='select id,name from Meeting__c';

        return Database.getQueryLocator (str);
    }



    public void execute(Database.BatchableContext bc, List<Meeting__c>laps){

        Integer meetCount  = laps.size();

        Integer attnCount = 0;

        for(Meeting__c lap :laps){

            List<Attendee__C> attList=new List<Attendee__c>();
            attList=[Select Id,Name,Attendee_Email__c,Country__c,Status__c,Attendee__c.Meeting__c from Attendee__c where  Meeting__c=:lap.name];

            attnCount = attnCount + attList.size();
        }
        String msg = 'Meeting count '+ string.valueof(meetCount)+' Attendee count ' + string.valueof(attnCount);

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {'[email protected]'});
        mail.setReplyTo('[email protected]');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody(msg);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


    }



    public void finish(Database.BatchableContext bc){

        system.debug(count);


    }

}

Found this in the logs

39.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
09:19:04.0 (585748)|USER_INFO|[EXTERNAL]|0056F000006d5Em|[email protected]|Pacific Standard Time|GMT-08:00
09:19:04.0 (621914)|EXECUTION_STARTED
09:19:04.0 (628420)|CODE_UNIT_STARTED|[EXTERNAL]|01p6F000008JIhi|BatchApexBL
09:19:04.0 (2932738)|HEAP_ALLOCATE|[72]|Bytes:3
09:19:04.0 (2978342)|HEAP_ALLOCATE|[77]|Bytes:152
09:19:04.0 (2994481)|HEAP_ALLOCATE|[342]|Bytes:408
09:19:04.0 (3014939)|HEAP_ALLOCATE|[355]|Bytes:408
09:19:04.0 (3034099)|HEAP_ALLOCATE|[467]|Bytes:48
09:19:04.0 (3065061)|HEAP_ALLOCATE|[139]|Bytes:6
09:19:04.0 (9143546)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:78
09:19:04.0 (9306369)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:11
09:19:04.0 (9321291)|SYSTEM_METHOD_ENTRY|[15]|BatchableContextImpl.BatchableContextImpl()
09:19:04.0 (9325448)|STATEMENT_EXECUTE|[15]
09:19:04.0 (9335716)|SYSTEM_METHOD_EXIT|[15]|BatchableContextImpl
09:19:04.0 (9341610)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12
09:19:04.0 (9345888)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
09:19:04.0 (9352875)|VARIABLE_SCOPE_BEGIN|[31]|this|Database.BatchableContextImpl|true|false
09:19:04.0 (9409439)|VARIABLE_ASSIGNMENT|[31]|this|{}|0x2b16200
09:19:04.0 (9415695)|VARIABLE_SCOPE_BEGIN|[31]|jobId|Id|false|false
09:19:04.0 (9526011)|VARIABLE_ASSIGNMENT|[31]|jobId|"7076F00006prWJ8QAM"
09:19:04.0 (9532894)|VARIABLE_SCOPE_BEGIN|[31]|childJobId|Id|false|false
09:19:04.0 (9541392)|VARIABLE_ASSIGNMENT|[31]|childJobId|null
09:19:04.0 (29055006)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:71
09:19:04.0 (29072438)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:7
09:19:04.0 (29084905)|METHOD_ENTRY|[1]|01p6F000008JIhi|BatchApexBL.BatchApexBL()
09:19:04.0 (29089787)|STATEMENT_EXECUTE|[1]
09:19:04.0 (29095835)|STATEMENT_EXECUTE|[1]
09:19:04.0 (29102946)|METHOD_EXIT|[1]|BatchApexBL
09:19:04.0 (29107060)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:20
09:19:04.0 (29109857)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12
09:19:04.0 (29117467)|VARIABLE_SCOPE_BEGIN|[35]|this|BatchApexBL|true|false
09:19:04.0 (29184443)|VARIABLE_ASSIGNMENT|[35]|this|{"attnCount":16,"count":0,"meetCount":17,"msg":"Meeting count 17 Att (14 more) ..."}|0x70da6e50
09:19:04.0 (29194118)|VARIABLE_SCOPE_BEGIN|[35]|bc|Database.BatchableContext|true|false
09:19:04.0 (29307959)|VARIABLE_ASSIGNMENT|[35]|bc|{"jobId":"7076F00006prWJ8QAM"}|0x2b16200
09:19:04.0 (29316590)|STATEMENT_EXECUTE|[35]
09:19:04.0 (29318223)|STATEMENT_EXECUTE|[37]
09:19:04.0 (29385888)|VARIABLE_SCOPE_BEGIN|[37]|mail|Messaging.SingleEmailMessage|true|false
09:19:04.0 (29442628)|VARIABLE_ASSIGNMENT|[37]|mail|"common.api.soap.wsdl.SingleEmailMessage@56bb0c29"|0x5bdacc6
09:19:04.0 (29449982)|STATEMENT_EXECUTE|[38]
09:19:04.0 (29491406)|HEAP_ALLOCATE|[38]|Bytes:4
09:19:04.0 (29535017)|HEAP_ALLOCATE|[38]|Bytes:23
09:19:04.0 (29579384)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
09:19:04.0 (29599236)|STATEMENT_EXECUTE|[39]
09:19:04.0 (29603173)|HEAP_ALLOCATE|[39]|Bytes:22
09:19:04.0 (29649172)|STATEMENT_EXECUTE|[40]
09:19:04.0 (29653590)|HEAP_ALLOCATE|[40]|Bytes:16
09:19:04.0 (29675168)|STATEMENT_EXECUTE|[41]
09:19:04.0 (29678571)|HEAP_ALLOCATE|[41]|Bytes:23
09:19:04.0 (29697580)|STATEMENT_EXECUTE|[42]
09:19:04.0 (29795458)|STATEMENT_EXECUTE|[43]
09:19:04.0 (29833900)|HEAP_ALLOCATE|[43]|Bytes:4
09:19:04.0 (29913417)|HEAP_ALLOCATE|[50]|Bytes:5
09:19:04.0 (29929470)|HEAP_ALLOCATE|[56]|Bytes:5
09:19:04.0 (29937760)|HEAP_ALLOCATE|[64]|Bytes:7
09:19:04.0 (29976773)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
09:19:04.0 (30013655)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
09:19:04.0 (115463466)|EXCEPTION_THROWN|[43]|System.EmailException: SendEmail failed. First exception on row 0; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: []
09:19:04.0 (117604166)|HEAP_ALLOCATE|[43]|Bytes:114
09:19:04.0 (117703862)|FATAL_ERROR|System.EmailException: SendEmail failed. First exception on row 0; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: []

Class.BatchApexBL.finish: line 43, column 1
3
  • 2
    Is your Email Deliverability settings is set to no access?
    – Raul
    Commented Feb 14, 2017 at 16:28
  • 1
    What batch size are you setting? You run a query for each record in scope so it seems like it should hit a LimitException.
    – Adrian Larson
    Commented Feb 14, 2017 at 16:53
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Commented Feb 14, 2017 at 17:35

2 Answers 2

1

I would create an excel CSV with all the meeting details, and attach it to single email and send in finish method; instead of sending an email per execute method, which doesn't even make sense.

With a quick google search with error, you would notice that you're hitting email limit in Salesforce.

Quoting from the post:

Using the API or Apex, you can send single emails to a maximum of 1,000 external email addresses per day based on Greenwich Mean Time (GMT). Single emails sent using the Salesforce application don’t count toward this limit.There s no limit on sending individual emails to contacts, leads, person accounts, and users in your organization directly from account, contact, lead, opportunity, case, campaign, or custom object pages

Note: SOQL inside a for loop is a bad practise.

Psuedo code:

public class BatchApexBL implements Database.batchable<Sobject>,Database.Stateful{

    public integer count = 0;
    String strCsvBody = '';

    public Database.QueryLocator start(Database.BatchableContext bc){
        String str = 'select id,name from Meeting__c';
        return Database.getQueryLocator (str);
    }

    public void execute(Database.BatchableContext bc, List<Meeting__c>laps){
        // collect each rows
        for(iterate over meeting and attendee) {
            strCsvBody += intMeetingCount + ',' + intAttendeeCount + '\n';;
        }
    }

    public void finish(Database.BatchableContext bc){
        // add header to CSV
        strCsvBody += 'Meeting, Attendee\n'; + strCsvBody;

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // adding CSV attachment 
        mail.setFileName(Meeting with Attendees.csv);
        mail.setBody(Blob.valueOf(strCsvBody));

        mail.setToAddresses(new String[] {'[email protected]'});
        mail.setReplyTo('[email protected]');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody(msg);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
1

Seems like it might be a silently swallowed governor limit issue. You have a query in your loop, which you should never do under any circumstances. In this case, it's easy to remove. Also, you don't need any query fields. Just use a count query.

Set<String> names = new Set<String>();
for (Meeting__c lap : laps) names.add(lap.Name);
Integer attnCount = [SELECT count() FROM Attendee__c WHERE Meeting__c IN :names];

Also note that email messages sent to a specific user are not subject to daily limits when specified by Id, but they are when specified by to address.

  • If you use SingleEmailMessage to email your org’s internal users, specifying the user’s ID in setTargetObjectId means the email doesn’t count toward the daily limit. However, specifying internal users’ email addresses in setToAddresses means the email does count toward the limit.

Since you are sending these messages to yourself, simply set the target:

mail.setToAddresses(new String[] {'[email protected]'});
// no bueno
mail.setTargetObjectId(UserInfo.getUserId());
// use this instead
4
  • Modified the code accordingly. But still the same issue persists. Commented Feb 14, 2017 at 17:14
  • As per her debug logs, She is hitting email limit- System.EmailException: SendEmail failed. First exception on row 0; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: []
    – Raul
    Commented Feb 14, 2017 at 18:37
  • 1
    Yeah, I see that in the edited post. I agree that it's probably a daily limit that's been reached.
    – Adrian Larson
    Commented Feb 14, 2017 at 18:41
  • It takes more investigation to answer a incomplete question or without an error message! :(
    – Raul
    Commented Feb 14, 2017 at 18:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .