0

I have two custom objects: Parent__c and Student__c with Master-detail relationship. Parent__c object has Email__c field. I need to send email to Parent__c with link(s) to Student__c object(s). I have code which sends two emails if Parent__c has 2 students. How to refactor code to send 1 email with 2 students links?

global class SendEmailToParent implements Schedulable{

global void execute(SchedulableContext sc){

        String subject = 'Student report';
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for(Parent__c parent : [SELECT Id, Name, Email__c, (SELECT Id, Name FROM Students__r) FROM Parent__c]){
            for(Student__c student : parent.Students__r){
                if(parent.Email__c != null){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    List<String> sendTo = new List<String>();
                    sendTo.add(parent.Email__c);
                    //mail.setTargetObjectId( UserInfo.getUserId() );
                    //mail.setSaveAsActivity( false );
                    mail.setToAddresses(sendTo);
                    mail.setSenderDisplayName('Director');
                    mail.setSubject(subject);
                    String body = 'Dear ' + parent.Name + ', ' + 'follow link to see report about your student ' +
                        'https://parent-report-developer-edition.eu12.force.com/?id=' + student.Id;
                    mail.setPlainTextBody(body);
                    mailList.add(mail);
                    system.debug(mailList);
                    system.debug(sendTo);
                }
            }
        }
        Messaging.sendEmail(mailList);
    }
}

1 Answer 1

0

You basically just need to move the location of your for loop, and accumulate links as part of a body variable. You'd do all the core email creation at the Parent level, but build up the plain text body by iterating over the Students. So you're already 90% of the way there.

Something like this should do the trick:

if(parent.Email__c != null){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    List<String> sendTo = new List<String>();
    sendTo.add(parent.Email__c);
    //mail.setTargetObjectId( UserInfo.getUserId() );
    //mail.setSaveAsActivity( false );
    mail.setToAddresses(sendTo);
    mail.setSenderDisplayName('Director');
    mail.setSubject(subject);

    String body = 'Dear ' + parent.Name + ', ' + 'follow link to see report about your student ';

    for(Student__c student : parent.Students__r){
        body += '\nhttps://parent-report-developer-edition.eu12.force.com/?id=' + student.Id;
    }

    mail.setPlainTextBody(body);
    mailList.add(mail);
    system.debug(mailList);
    system.debug(sendTo);
}

You might want to clean that up a bit to produce a more appealing email, but I think the core is there.

0

You must log in to answer this question.

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