0

Can I access a lead's email history in Apex to see if they've already been sent an email from a timeframe/template combination? Or even something arbitrary like the email subject?

public void send() {
    EmailTemplate emailTemplate = [SELECT Id, Name FROM EmailTemplate WHERE Name = 'Whatever'];
    List<Messaging.SingleEmailMessage> emailBlast = new List<Messaging.SingleEmailMessage>();
    List<Lead> leads = [SELECT Id, Email FROM Lead WHERE LastName = 'Recipients'];

    for (Lead lead: leads) {
        // if lead.emails includes 'Whatever' ??
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'[email protected]'};
            mail.setToAddresses(toAddresses);
            mail.setTargetObjectId(lead.Id);
            mail.setTemplateId(emailTemplate.Id);
            emailBlast.add(mail);
        // end if
    }
    Messaging.SendEmailResult[] blastResults = Messaging.sendEmail(emailBlast);
}

Any ideas? If my question was unclear the comments in the code might help. Thanks.

2
  • Do you record your outbound emails as Activities underneath the Lead?
    – cropredy
    Jan 13, 2016 at 18:56
  • Yes, I sure do.
    – t56k
    Jan 13, 2016 at 20:43

1 Answer 1

1

If you store outbound Leads as Tasks within the Lead Activity History related list, then you can ascertain whether emails were sent within a given time period or with a specific subject simply by querying

List<Lead> leads = [select id, email ,
           (select id, activityDate, subject from ActivityHistories
              where subject like 'Email:%' and
                    activityDate >= :startDate and // some vbl you init
                    activityDate <= :endDate       // here too
           )
            from Lead where lastName = 'someLName'] ; 

for (Lead l : leadList) {
   if (l.activityHistories.size() > 0)
      // some previous email was sent that we want to exploit in our new email
      ...
}

Outbound emails Tasks are prefixed by Email: in the generated Subject field.

If you use templates, then the template has a predefined subject and you can use that to make the like 'expression' more sophisticated to match the template. Of course, templates should have unique subjects for this to work

As an aside, you should, before sending the email, make sure there is outbound email capacity to send the email. See related stackexchange answer. SFDC is not designed to do APEX-driven email blasts so outbound limits are severe/strict depending on org license (e.g. 1000/day for Enterprise). I note in your code that you are doing the outbound send for multiple emails so that could be an issue - even if your blast works, you may gobble up outbound email capacity for other use cases.

1
  • Dude, ultra informative. Thanks so much. Our email needs are pretty minimal for this blast so limits are okay. Thanks!
    – t56k
    Jan 13, 2016 at 21:37

You must log in to answer this question.

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