You need to sign in to do that
Don't have an account?

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>();
//loop
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
singleMail.setTargetObjectId(con.Id);
//set template Id
singleMail.setTemplateId(et.Id);
//flag to false to stop inserting activity history
singleMail.setSaveAsActivity(false);
//add mail
emails.add(singleMail);
}
}
//send mail
Messaging.sendEmail(emails);
return accounts;
}
}
(test class)
@isTest(SeeAllData=true)
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];
test.startTest();
HelperContactTrigger.sendEmail(sendMail);
test.stopTest();
System.assert(acc !=null);
}
}
}
(trigger)
trigger MailAusloeser on Account (after insert) {
if(Trigger.isAfter){
if(Trigger.isInsert ){
//helper class for single email but bulk messages
HelperContactTrigger.sendEmail(trigger.new);
}
}
}
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>();
//loop
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
singleMail.setTargetObjectId(con.Id);
//set template Id
singleMail.setTemplateId(et.Id);
//flag to false to stop inserting activity history
singleMail.setSaveAsActivity(false);
//add mail
emails.add(singleMail);
}
}
//send mail
Messaging.sendEmail(emails);
return accounts;
}
}
(test class)
@isTest(SeeAllData=true)
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];
test.startTest();
HelperContactTrigger.sendEmail(sendMail);
test.stopTest();
System.assert(acc !=null);
}
}
}
(trigger)
trigger MailAusloeser on Account (after insert) {
if(Trigger.isAfter){
if(Trigger.isInsert ){
//helper class for single email but bulk messages
HelperContactTrigger.sendEmail(trigger.new);
}
}
}
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