function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shekhar 46shekhar 46 

send email on record update by apex

I want to send email to trainer dept and bcc to CEO, and want to share the data of training object in object 
 
Best Answer chosen by shekhar 46
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shekhar,

I have tried the below code in my org.. its working fine.
trigger Sendemail on training_deal__c (before insert) {
 // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
       //Set list of people who should get the email
      List<String> sendTo = new List<String>();
  List<User> userlist = [select id,email from user where Profile.Name='System Administrator'];
  
  for(user u:userlist){
  sendTo.add(u.email);
  }
  
  for (training_deal__c myContact : Trigger.new) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      //Step 2: Set list of people who should get the email
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setSenderDisplayName('New Training Deal created');
    
      // (Optional) Set list of people who should be BCC'ed
      List<String> Bccto = new List<String>();
      Bccto.add('abandi@salesforce.com');
      mail.setBccAddresses(Bccto);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Training Deal created');
      String body = myContact.Name;
      body += 'Training deal Record has been created';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);

}
If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Shekar,

when you need to send an email to trainer. ?

Can you explain more about your requirement?

Thanks!!
shekhar 46shekhar 46
@Ankaiah, whenever record is created at training deal object,  mail need to send to training dept's user . and ceo@abc.com need to be in bcc.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shekhar,

Do you have the field called training dept user in training deal object?

Thanks!!
shekhar 46shekhar 46
no it is not a field, means i need to send email for users present in training dept . training dept is profile, and there are 2 users in it, need to send email to those users and bcc to ceo
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shekhar,

try with below code and modify the object api name as per your org.
trigger Sendemail on training_deal__c(before insert){

// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
       Set list of people who should get the email
      List<String> sendTo = new List<String>();
  List<User> userlist = [select id,email from user where where Profile.Name='Training Department'];
  
  for(user u:userlist){
  sendTo.add(u.email);
  }
  
  for (training_deal__c myContact : Trigger.new) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      //Step 2: Set list of people who should get the email
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setSenderDisplayName('New Training Deal created');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('ceo@yourcompany.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Training Deal created');
      String body = myContact.Name;
      body += 'Training deal Record has been created';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

If this helps, Please mark it as best answer.

Thanks!!
shekhar 46shekhar 46
if we used setCCaddress and send mail to ceo, does others from trainning dept will know ? 
I want that Whenever mail is going to ceo , nobody should know , hence we want to keep in bcc.
AnkaiahAnkaiah (Salesforce Developers) 
Then you can use the setBccAddresses instead of setCcAddresses.

try with below code.
trigger Sendemail on training_deal__c(before insert){

// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
       Set list of people who should get the email
      List<String> sendTo = new List<String>();
  List<User> userlist = [select id,email from user where where Profile.Name='Training Department'];
  
  for(user u:userlist){
  sendTo.add(u.email);
  }
  
  for (training_deal__c myContact : Trigger.new) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      //Step 2: Set list of people who should get the email
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setSenderDisplayName('New Training Deal created');
    
      // (Optional) Set list of people who should be Bcc'ed
      List<String> BccTo = new List<String>();
      ccTo.add('ceo@yourcompany.com');
      mail.setBccAddresses(BccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Training Deal created');
      String body = myContact.Name;
      body += 'Training deal Record has been created';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

If this helps, please mark it as best answer.

Thanks!!
 
shekhar 46shekhar 46
i tried this with changiing API, but still giving erro
trigger
trigger sendMail on Training_deal__c (before insert) {
SendMailTrigger.SendMailFunction(trigger.new);
}
 class
public class SendMailTrigger {
    public static void SendMailFunction( List<Messaging.SingleEmailMessage> VarMails ){
       // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
      // Set list of people who should get the email
      List<String> sendTo = new List<String>();
  List<User> userlist = [select id,email from user where Profile.Name='Training dept'];
  
  for(user u:userlist){
  sendTo.add(u.email);
  }
  
  for (training_deal__c myContact : Trigger.new) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      //Step 2: Set list of people who should get the email
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setSenderDisplayName('New Training Deal created');
    
      // (Optional) Set list of people who should be Bcc'ed
      List<String> BccTo = new List<String>();
      ccTo.add('ggaikwad25794@gmail.com');
      mail.setBccAddresses(BccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Training Deal created');
      String body = myContact.Name;
      body += 'Training deal Record has been created';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(VarMails);
}
}
}
}
error
invalid character, invalid data type varmail



 
AnkaiahAnkaiah (Salesforce Developers) 
you have placed wrong value VarMails in the below code.
worng:
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(VarMails);

correct:
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(Mails);

Modify the code and check.

Thanks!!

 
shekhar 46shekhar 46
i changed varMailes with Mails,  giving error of invalid type: Mails 
error: variable does not exist :ccTo
AnkaiahAnkaiah (Salesforce Developers) 
Can  you share your code?

Thanks!!
AnkaiahAnkaiah (Salesforce Developers) 
try with below code.
trigger Sendemail on training_deal__c(before insert){

// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
       Set list of people who should get the email
      List<String> sendTo = new List<String>();
  List<User> userlist = [select id,email from user where where Profile.Name='Training Department'];
  
  for(user u:userlist){
  sendTo.add(u.email);
  }
  
  for (training_deal__c myContact : Trigger.new) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      //Step 2: Set list of people who should get the email
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setSenderDisplayName('New Training Deal created');
    
      // (Optional) Set list of people who should be Bcc'ed
      List<String> BccTo = new List<String>();
      BccTo.add('ceo@yourcompany.com');
      mail.setBccAddresses(BccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Training Deal created');
      String body = myContact.Name;
      body += 'Training deal Record has been created';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

Let me know, still facing an issue.

If this helps, Please mark it as best answer.

Thanks!!
shekhar 46shekhar 46
this above code showing erros as ccTo variable does not exist, invalid constructor name messaging.sendEmail, 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shekhar,

I have tried the below code in my org.. its working fine.
trigger Sendemail on training_deal__c (before insert) {
 // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
       //Set list of people who should get the email
      List<String> sendTo = new List<String>();
  List<User> userlist = [select id,email from user where Profile.Name='System Administrator'];
  
  for(user u:userlist){
  sendTo.add(u.email);
  }
  
  for (training_deal__c myContact : Trigger.new) {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      //Step 2: Set list of people who should get the email
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setSenderDisplayName('New Training Deal created');
    
      // (Optional) Set list of people who should be BCC'ed
      List<String> Bccto = new List<String>();
      Bccto.add('abandi@salesforce.com');
      mail.setBccAddresses(Bccto);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Training Deal created');
      String body = myContact.Name;
      body += 'Training deal Record has been created';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);

}
If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer
shekhar 46shekhar 46
Hi Ankaiah  ,
Thanks , now it is working fine. I was trying to write logic in apex class not is trigger, in that case was getting error. 
this code is working directly.