3

I'm working on a trigger to notify a user when there email changes, but am having trouble compiling it. It's saying a variable doesn't existing, what am I doing wrong?

trigger email on Contact (before insert) {

    for(Contact c : Trigger.new){
     Contact old = trigger.oldMap.get(c.Id);
        if(old.Email != c.Email){
         Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();

            mail.setToAddresses(new String[] {c.Email});
            mail.setSubject('Test Mail Trigger');
            mail.setPlainTextBody('Hi  you have changed your email Id'+ old.Email +'to ' + c.Email);
        }
     }
    Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail}); **//Here I am getting error it says "Variable does not exists:mail"**
}
6

The reason you're getting the error message is because you have initialized the mail inside the If condition and you're referencing it outside the if condition. Also you're trigger was not bulkified. So first you should store your mail object in a list of type messaging.singleSendEmail and then invoke the messaging.sendEmail() method outside the for loop because there's a limit of 10 invocation to the messaging.sendEmail() method in a single apex execution.

list<Messaging.SingleEmailMessage> mailList = new list<Messaging.SingleEmailMessage>();
for(Contact c : Trigger.new){
 Contact old = trigger.oldMap.get(c.Id);
    if(old.Email != c.Email){
     Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();

        mail.setToAddresses(new String[] {c.Email});
        mail.setSubject('Test Mail Trigger');
        mail.setPlainTextBody('Hi  you have changed your email Id'+ old.Email +'to ' + c.Email);
        mailList.add(mail);
    }
 }
Messaging.sendEmail(mailList);
  • Thank.. for that "List" advice.....but now it shows same error in 3rd line 'c.Id' variable does not exists – user40138 Nov 30 '13 at 15:51
  • 1
    Try using before update for executing code that detects a change. The before insert is fired before the initial insert/creation of the record and not on subsequent updates. – Peter Knolle Nov 30 '13 at 18:43
0

You are referencing ID of a record which is not yet created.A Record have ID value only after creation.

So please run your trigger after update event.

This will solve your problem.

  trigger email on Contact (after update) {
  Messaging.SingleEmailMessage mail;

  List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
  for(Contact c : Trigger.new){
     Contact old = trigger.oldMap.get(c.Id);
     if(old.Email != c.Email){

         mail= new Messaging.SingleEmailMessage();

         mail.setToAddresses(new String[] {c.Email});
         mail.setSubject('Test Mail Trigger');
         mail.setPlainTextBody('Hi  you have changed your email Id'+ old.Email +'to ' + c.Email);
         messages.add(mail);
    }
 }
Messaging.sendEmail(messages); 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.