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

how to send an automatic email when lead status is converted to "Qualified"
Hello all
I want to send an automatic email when lead status is converted to "Qualified" , i tried it in workflow and process, it worked fine, now i want to try it in apex , i have ready made email template named "XXX" which shows as
Hello {!Lead.LastName}
Thank you for the registering with us
Soon we will get back to you
Team EduProserra
my apex class is
public class emailSending {
public String Lead { get; set; }
public string toMail { get; set;}
public string ccMail { get; set;}
public string repMail { get; set;}
public void sendMail(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string[] to = new string[] {toMail};
string[] cc = new string[] {ccMail};
email.setToAddresses(to);
if(ccMail!=null && ccMail != '')
email.setCcAddresses(cc);
if(repmail!=null && repmail!= '')
email.setInReplyTo(repMail);
email.setSubject('Thank you for the Registration');
email.setHtmlBody('Hello, {!Lead.LastName} <br/><br/>Thank you for Registration. <br/>We will get back to you for more details<br/><br/>Regards<br/> EduPro Team');
try{
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
}catch(exception e){
apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getMessage()));
}
toMail = '';
ccMail = '';
repMail = '';
}
}
how do i design a trigger for this??
I want to send an automatic email when lead status is converted to "Qualified" , i tried it in workflow and process, it worked fine, now i want to try it in apex , i have ready made email template named "XXX" which shows as
Hello {!Lead.LastName}
Thank you for the registering with us
Soon we will get back to you
Team EduProserra
my apex class is
public class emailSending {
public String Lead { get; set; }
public string toMail { get; set;}
public string ccMail { get; set;}
public string repMail { get; set;}
public void sendMail(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string[] to = new string[] {toMail};
string[] cc = new string[] {ccMail};
email.setToAddresses(to);
if(ccMail!=null && ccMail != '')
email.setCcAddresses(cc);
if(repmail!=null && repmail!= '')
email.setInReplyTo(repMail);
email.setSubject('Thank you for the Registration');
email.setHtmlBody('Hello, {!Lead.LastName} <br/><br/>Thank you for Registration. <br/>We will get back to you for more details<br/><br/>Regards<br/> EduPro Team');
try{
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
}catch(exception e){
apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getMessage()));
}
toMail = '';
ccMail = '';
repMail = '';
}
}
how do i design a trigger for this??
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Trigger:
Handler Class:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
In the code you posted you will be creating exponentially duplicate emails. You are both iterating through the list of Leads and then calling the sendEmail method and also pushing through the entire list into the method where it's contents are iterated through a second time.
10 leads in the trigger could create 100 emails. Instead move the conditional out of the trigger and into the class and change this to this Also, change the return type of the sendEmail method to void since there is no reason to return anything since the trigger will not be using any returned values.
Here is the updated code:
Trigger:
Handler:
Regards,
Khan Anas
Try this code:
Trigger:
trigger LeadTriggerSendMail on Lead (before insert, before update) {
if((trigger.isInsert && trigger.isBefore) || (trigger.isUpdate && trigger.isBefore)) {
LeadTriggerSendMail_handler.checkLeadStatus(trigger.new);
}
}
Handler:
public class LeadTriggerSendMail_handler {
public static void checkLeadStatus(List<Lead> leadList) {
try {
List<String> emailList = new List<String>();
for(Lead l : leadList) {
if(l.Email != null && l.Status == 'Qualified') {
emailList.add(l.Email);
EmailTemplate temp = [SELECT Id FROM EmailTemplate WHERE developername = 'XXX' LIMIT 1];
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(l.Id);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
} catch (Exception ex) {
system.debug('Exception---ofLine--->' + ex.getLineNumber());
system.debug('Exception---Message--->' + ex.getMessage());
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
i am trying do the same on the contact object now, 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 have designed a trigger and apex class, but i do get an error
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);
}
}
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
i know this is something to do with collections - should i take map instead list here
Hello,
I did tried this in my org !! You can also try this code !!
For the Helper Code :For the Trigger :
with regards
https://plex.software (https://plex.software) https://kodi.software/ https://luckypatcher.pro/
Here is the code for Contact, try this code:
Thanks,
Ajay Dubedi
It easy to send an email through apex code using a trigger. Please try the below code:-
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com