0

I am trying to send email to nag our agents to finsh opportunity data entry. I am running into an issue I haven't been about to work out. I am getting zero errors and sending zero emails. Please any help would be greatly appreciated.

Global class MarketingNag implements Schedulable {

    global void execute(SchedulableContext SC) {
        List<Opportunity> ListingOps = New List<opportunity>();
        ListingOps = [select id, Agent__c, name from opportunity where agency_type__C ='seller' AND StageName ='coming soon' AND (Bedrooms__c = null OR Bedrooms__c = null OR Marketing_Description_Populated__c = false ) limit 10];
        EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'New Listing Coming Soon'];

        For(Opportunity LOPS : ListingOps){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Marketing');
            mail.setTargetObjectId(LOPS.Agent__c); 
            mail.setTemplateId(et.id);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

        }
    }
}

The Agent__c field is a lookup enter image description here

There is another field Accociate_contact_identer image description here

I have tried both fields to get the agents email into the templates to address.

  • Have you actually scheduled this class? When does it run? Did you check the Apex Job log and see zero errors there? – Adrian Larson Feb 9 '18 at 0:01
  • I have not scheduled it yet. – Roger Morris Feb 9 '18 at 0:08
  • So how do you expect it to send emails? Please edit your question to be more clear. – Adrian Larson Feb 9 '18 at 0:09
  • I have been running it from the developer console. MarketingNag nager = new MarketingNag(); – Roger Morris Feb 9 '18 at 0:13
  • 1
    Did you check your email deliverability settings? – Adrian Larson Feb 9 '18 at 0:19
1

Your code won't run because the EmailTemplate query will always return zero rows, causing a QueryException.

DeveloperName cannot contain spaces, start with a number, or have more than one consecutive underscore.

As such, your query should look more like:

    EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'New_Listing_Coming_Soon'];

Secondly, never call Messaging.sendEmail in a loop. Collect all the emails into a list, then send them:

    Messaging.SingleEmailMessage[] mails = new Messaging.SingleEmailMessage[0];

    For(Opportunity LOPS : ListingOps){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSenderDisplayName('Marketing');
        mail.setTargetObjectId(LOPS.Agent__c); 
        mail.setTemplateId(et.id);
        mails.add(mail);
    }
    Messaging.sendEmail(mails);

Third, if you're emailing users, you need to set SaveAsActivity flag to false, or it will fail to send:

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSenderDisplayName('Marketing');
        mail.setTargetObjectId(LOPS.Agent__c); 
        mail.setSaveAsActivity(false);
        mail.setTemplateId(et.id);
        mails.add(mail);

Fourth, you're not checking for duplicates, so if the users are duplicated, they'll get lots of identical emails. I'd suggest creating a Set, then emailing them:

    Set<Id> userIds = new Set<Id>();
    For(Opportunity LOPS : ListingOps){
        userIds.add(LOPS.Agent__c);
    }
    for(Id userId: userIds) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSenderDisplayName('Marketing');
        mail.setTargetObjectId(userId); 
        mail.setSaveAsActivity(false);
        mail.setTemplateId(et.id);
        mails.add(mail);
    }

Finally, based on your comments, you didn't actually call the "execute" method, so you're not actually running the code you wrote. An execute anonymous script should look like:

new MarketingNag().execute(null);

Side note: If you expect more than opportunities to possibly meet your criteria, consider removing the LIMIT or making it higher. Otherwise, you're only going to nag some of your users. Also, make sure you add a filter so you only query active users, since you can't email inactive users.

| improve this answer | |
  • Thanks I will start nocking all these out. I really appreciate your feedback. – Roger Morris Feb 9 '18 at 0:24
  • That got me 90% email delivered content not exactly right THANK SO MUCH! – Roger Morris Feb 9 '18 at 0:34
  • Pro tip...SingleEmailMessage has a default limit of 10 messages per sendEmail() call (may vary by org). You WILL get an exception if you exceed your org's limit. I use "List<List<Messaging.SingleEmailMessage>>" and a for() loop to send in batches of 10. – Ixalmida Aug 7 '19 at 20:51
  • @Ixalmida Not so, see this answer. I have successfully sent 10,000 emails in a single transaction. You are allowed 10 sendEmail calls, but the list size is not limited. – sfdcfox Aug 7 '19 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.