+ Start a Discussion
Renuka SharmaRenuka 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);
    }
}


 
Best Answer chosen by Renuka Sharma
Ajay K DubediAjay K Dubedi
Hi Manjunath,
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:
trigger emailfeesurvey on Contact (before insert, before update) {
    if((trigger.isInsert && trigger.isBefore) || (trigger.isUpdate && trigger.isBefore)) {
        emailfeesurvey_handler.checkfeesurvey(trigger.oldMap, trigger.new);
    }
}

Handler:
public class emailfeesurvey_handler {
    public static void checkfeesurvey(Map<Id, Contact> conOldMap, List<Contact> contactList) {
        try {
            EmailTemplate temp = [SELECT Id FROM EmailTemplate WHERE developername = 'Course_complete_survey' LIMIT 1];
            List<String> emailList = new List<String>();
            for(contact c : contactList) {
                if(c.Email != null && c.Course_completed__c == True && conOldMap.get(c.Id).Course_completed__c != c.Course_completed__c) {
                    emailList.add(c.Email);
                }
            }
            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.setSubject('Testing');
                String body = 'This mail is use for testing.';
                mail.setHtmlBody(body);
                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

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Please use below class and let me know if it helps. It should be checkbox__c instead of checkbox because this is a custom field. Thanks.
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__c == '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());
        }
    }
}

 
Ravi Dutt SharmaRavi Dutt Sharma
One more point: make it c.checkbox__c == true. No quotes around true.
Renuka SharmaRenuka Sharma
@ ravi

i am getting an error -  Variable does not exist: checkbox__c

should i include API name or datatype??


User-added image
Ravi Dutt SharmaRavi Dutt Sharma
Make it c.Couse_completed__c
Renuka SharmaRenuka Sharma
Now i am  getting error from trigger and apex class

User-added image

 
Ravi Dutt SharmaRavi Dutt Sharma
I can see a type in the third error. It course, not couse.
Ajay K DubediAjay K Dubedi
Hi Manjunath,
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:
trigger emailfeesurvey on Contact (before insert, before update) {
    if((trigger.isInsert && trigger.isBefore) || (trigger.isUpdate && trigger.isBefore)) {
        emailfeesurvey_handler.checkfeesurvey(trigger.oldMap, trigger.new);
    }
}

Handler:
public class emailfeesurvey_handler {
    public static void checkfeesurvey(Map<Id, Contact> conOldMap, List<Contact> contactList) {
        try {
            EmailTemplate temp = [SELECT Id FROM EmailTemplate WHERE developername = 'Course_complete_survey' LIMIT 1];
            List<String> emailList = new List<String>();
            for(contact c : contactList) {
                if(c.Email != null && c.Course_completed__c == True && conOldMap.get(c.Id).Course_completed__c != c.Course_completed__c) {
                    emailList.add(c.Email);
                }
            }
            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.setSubject('Testing');
                String body = 'This mail is use for testing.';
                mail.setHtmlBody(body);
                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
This was selected as the best answer
Renuka SharmaRenuka Sharma
@ Thank you Mr Ravi dutt Sharma and Mr Ajay K Dubedi