0

I would like to schedule an email to send out everyday at 8am to all contacts that meet the requirements:

account_expiration__c != null

I wrote a schedulable apex class but i reach singleEmailMessage limits. (Provided below)

I looked into using the massEmailMessage class, but cannot impliment as i need the email to come from a specific user based on ownership and there is no setOrgWideEmailAddressId() method. So my conclusion is i cannot utilize this class.

I then thought that i may be able to utilize a batch class, but i really cant find any example of anyone using it for something other then to iterate through sObjects.

My question is: is there any way that i haven’t explored to accomplish my goal? Maybe an easier way. (Im a novice and tend to take the hard way)

or

Am i on the correct path using a batchable class? And if so can someone please show me an example of how one would batch singleEmailMessage?

global class dailyEmail implements Schedulable {
global void execute(SchedulableContext sc) {

    //querying all approved Contacts
    list<Account> lstApprovedAcct = [SELECT ID FROM Account WHERE Account_Status__c = 'Approved'];
    list<Contact> lstApprovedCon = [SELECT ID, Owner.Email FROM Contact WHERE HasOptedOutOfEmail = false AND Email != null AND Contact.AccountID IN :lstApprovedAcct];

    //getting org wide email address which is used in template email as whom it will be sending to. 
    OrgWideEmailAddress lorin = [SELECT ID, Address FROM OrgWideEmailAddress WHERE Address = '[email protected]' LIMIT 1];
    OrgWideEmailAddress jason = [SELECT ID, Address FROM OrgWideEmailAddress WHERE Address = '[email protected]' LIMIT 1];

    // select email template    
    EmailTemplate lorinTemplate = [SELECT Name FROM EmailTemplate WHERE Name = 'Daily Email - Lorin' limit 1];
    EmailTemplate jasonTemplate = [SELECT Name FROM EmailTemplate WHERE Name = 'Daily Email - Jason' limit 1];

    //List of Emails to be sent, after iteration of orgWideIDs are added as individuals.
    list<Messaging.SingleEmailMessage> lstEmailJason = new list<Messaging.SingleEmailMessage>();

    //Lorin list of emails to send
    list<Messaging.SingleEmailMessage> lstEmailLorin = new list<Messaging.SingleEmailMessage>();

    //loop through each contact and send a single templated message
    for(Contact iterator : lstApprovedCon) {
        if(iterator.owner.Email == jason.Address) {
            Messaging.SingleEmailMessage jayMail = new Messaging.SingleEmailMessage();
            jayMail.setTargetObjectId(iterator.Id); //contact ID
            jayMail.setOrgWideEmailAddressId(jason.ID); // from address
            jayMail.setTemplateId(jasonTemplate.Id); //template ID
            jayMail.setUseSignature(false);
            lstEmailJason.add(jayMail);
        }   
        else if (iterator.owner.Email == lorin.Address) {
            Messaging.SingleEmailMessage lorinMail = new Messaging.SingleEmailMessage();
            lorinMail.setTargetObjectId(iterator.Id); //contact ID
            lorinMail.setOrgWideEmailAddressId(lorin.ID); // from address
            lorinMail.setTemplateId(lorinTemplate.Id); //template ID
            LorinMail.setUseSignature(false);
            lstEmailLorin.add(lorinMail);
        }
    }
    if(!Test.isRunningTest()){
        if(!lstEmailLorin.isEmpty()) 
            Messaging.sendEmail(lstEmailLorin); 
        if(!lstEmailJason.isEmpty()) 
            Messaging.sendEmail(lstEmailJason);
    }
}

1 Answer 1

4

A batch class is not going to change your email limit consumption, which is a daily total - it doesn't matter how you use it up. If you're sending more than 5,000 single or bulk messages per day and Salesforce is not willing to increase the limit for you, you'll need to look at integrating a bulk email application with Salesforce.

EDIT: Based upon discussion in comments, your class is not failing due to a limits error at all, but due to bad/bouncing email addresses. You may want to set allOrNothing to false in addition to addressing your data issues.

14
  • It doesnt reach salesforce mass email limits at all, in total there are 238 in use. If you follow the link it reaches singlememailmessage class limits of 100 addresses in the TO field per instance.
    – Bryan
    Commented Dec 29, 2017 at 16:11
  • Please add the complete text of the error message you receive in your question, along with the line number where the error is thrown.
    – David Reed
    Commented Dec 29, 2017 at 16:18
  • No error message at all, its scheduled and works in sandbox but not in production. There are only 10 contacts that meet the requirements in sandbox and 238 in production, i found the limits page after i rolled out. Should i add a try catch?
    – Bryan
    Commented Dec 29, 2017 at 16:23
  • If you are not receiving an error message, it is hard to diagnose the issue. I do not believe that you are hitting the To address limit, because you are not adding multiple To addresses to any single message. You'll need to review your logs and email delivery settings to help determine what class of error you're dealing with.
    – David Reed
    Commented Dec 29, 2017 at 16:25
  • Im not sure how to find any error? I added a debug log for the class and rescheduled it. As the code is written now do i have to add debug stements?
    – Bryan
    Commented Dec 29, 2017 at 16:34

You must log in to answer this question.

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