+ Start a Discussion
Bernd NawrathBernd Nawrath 

Email Trigger

Hello dear community, I just want to make sure my code looks good to go. Basically I created an email trigger that I want to upload into the production now.
Its purpose is: When a new account is added, an email template with pdf attachment is supposed to be sent automatically to that person. Please have a look on the code and tell if you think that's going to do it because I couldnt test it in sandbox, just made sure it works with the test class:
Thanks a lot :)

(main class)
public class HelperContactTrigger {
    //static method
    public static List<Account> sendEmail(List<Account> accounts) {

        //query on template object
        EmailTemplate et=[Select id from EmailTemplate where name= 'MCM'];

        system.debug(et);   //debug to check the template

        //list of emails
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        for(Account con : accounts){

            //check for Account
            if(con.PersonEmail != '' && con.PersonEmail != null) {

                //initiallize messaging method
                Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

                //set object Id

                //set template Id

                //flag to false to stop inserting activity history

                //add mail

        //send mail

        return accounts;

(test class)
private class HelperContactTriggerTestneu {
public static testMethod void myTestMethod() {

        system.debug('### NewAccountTest ###');
        Account acc = new Account(Name = 'Test Test');
        insert acc;

        List<Account> sendMail = [select id,PersonEmail from account where (Name='Test Test') and id=:acc.id];

        System.assert(acc !=null);

trigger MailAusloeser on Account (after insert) {
        if(Trigger.isInsert ){ 
            //helper class for single email but bulk messages

Shashikant SharmaShashikant Sharma
Code looks good.

Only few things

1. Account con : accounts => It looks you are using Account as person account
I would suggest to name the variable as personAcc or acc instead of con

2.  As you are sending email from code there is a limit of Single email in a context and within 24 hours. You could add check for these. Although it is very advance level of coding which is used mainly in managed packages. 

See Limits Class : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_limits.htm#apex_System_Limits_getEmailInvocations