You need to sign in to do that
Don't have an account?
Renuka Sharma
small help required on apex coding regarding email firing
Hello all
In the contact object there is a custom checkbox field called "Course_completed__c" when i check the checkbox field, it should fire an email, i know this can be achived through workflow and process builder, but i want to try in apex so i have designed a trigger and apex class, but i do get an error
the error i get is - Variable does not exist: checkbox and Method does not exist or incorrect signature: void checkfeesurvey(List<Contact>) from the type emailfeesurvey_handler
Apex class -
public class emailfeesurvey_handler {
public static void checkfeesurvey(List<contact> contactlist) {
try {
List<String> emailList = new List<String>();
for(contact c : contactList) {
if(c.Email != null && c.checkbox == 'True') {
emailList.add(c.Email);
EmailTemplate temp = [SELECT Id FROM EmailTemplate WHERE developername = 'Course_complete_survey' LIMIT 100];
system.debug('emailList------' + emailList);
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
if(emailList.size() > 0) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emailList);
mail.setTemplateId(temp.Id);
mail.setTargetObjectId(c.Id);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
} catch (Exception ex) {
system.debug('Exception---ofLine--->' + ex.getLineNumber());
system.debug('Exception---Message--->' + ex.getMessage());
}
}
}
Trigger -
trigger emailfeesurvey on Contact (before insert, before update) {
if((trigger.isInsert && trigger.isBefore) || (trigger.isUpdate && trigger.isBefore)) {
emailfeesurvey_handler.checkfeesurvey(trigger.new);
}
}
In the contact object there is a custom checkbox field called "Course_completed__c" when i check the checkbox field, it should fire an email, i know this can be achived through workflow and process builder, but i want to try in apex so i have designed a trigger and apex class, but i do get an error
the error i get is - Variable does not exist: checkbox and Method does not exist or incorrect signature: void checkfeesurvey(List<Contact>) from the type emailfeesurvey_handler
Apex class -
public class emailfeesurvey_handler {
public static void checkfeesurvey(List<contact> contactlist) {
try {
List<String> emailList = new List<String>();
for(contact c : contactList) {
if(c.Email != null && c.checkbox == 'True') {
emailList.add(c.Email);
EmailTemplate temp = [SELECT Id FROM EmailTemplate WHERE developername = 'Course_complete_survey' LIMIT 100];
system.debug('emailList------' + emailList);
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
if(emailList.size() > 0) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emailList);
mail.setTemplateId(temp.Id);
mail.setTargetObjectId(c.Id);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
} catch (Exception ex) {
system.debug('Exception---ofLine--->' + ex.getLineNumber());
system.debug('Exception---Message--->' + ex.getMessage());
}
}
}
Trigger -
trigger emailfeesurvey on Contact (before insert, before update) {
if((trigger.isInsert && trigger.isBefore) || (trigger.isUpdate && trigger.isBefore)) {
emailfeesurvey_handler.checkfeesurvey(trigger.new);
}
}
Never query inside the for loop in your code.
And you need to query only one template, so put limit 1 in the query. And you have also need to check that the mail only be send when your checkBox is true from false.
Try this trigger and handler class:
Trigger:
Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
i am getting an error - Variable does not exist: checkbox__c
should i include API name or datatype??
Never query inside the for loop in your code.
And you need to query only one template, so put limit 1 in the query. And you have also need to check that the mail only be send when your checkBox is true from false.
Try this trigger and handler class:
Trigger:
Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi