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
The new LearnerThe new Learner 

How to send an email alert when ever the record is created

HI Experts,

I want to send an email to queue members when ever the lead is created. when new lead is created its assigining to queue. now i want to send an email for queue users can anyone help me. thanks in advance
VinayVinay (Salesforce Developers) 
Hi,

You can use workflow rule or process builder to trigger an email alert whenever a record is created.

https://help.salesforce.com/articleView?id=000313053&type=1&mode=1
https://help.salesforce.com/articleView?id=customize_wfalerts.htm&type=5

Thanks,
The new LearnerThe new Learner
HI Vinay ,

Thanks for the response, in my case when ever i crated lead record the owner getting changing to queue and now i need to send and an email to those queue users
VinayVinay (Salesforce Developers) 
You can use criteria as whenever you create a new lead or you can use ISCHANGED function and select queue name in recipient column.  

https://trailblazers.salesforce.com/answers?id=90630000000gxmUAAQ

Thanks,
The new LearnerThe new Learner
HI Vinay,

can you help me in below code, i am receving three emails.
 
public with sharing class HelperTrigger {
    //static method
    public static void sendEmail(List<Lead> Leads) {

        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        list<string>listqueue=new list<string>();
        List<String> idList = new List<String>();
       
        for(Lead Led: Leads)
        {

            if(Led.Queue_Owner__c != null)
            {
             listqueue.add(Led.Queue_Owner__c);
             system.debug('@@listqueue'+listqueue);
            }
         }
         
        if( !listqueue.isEmpty() ) 
        {  
          Group gp = [SELECT (SELECT UserOrGroupId FROM GroupMembers) FROM Group where Group.Type = 'Queue' and id in:listqueue];
          system.debug('@@gp '+gp );
          
          if(gp !=null) 
          {
          
          for (GroupMember gm : gp.GroupMembers) 
           {
             idList.add(gm.UserOrGroupId);
          }
          
          List<User> userList = [SELECT Email,LanguageLocaleKey  FROM User WHERE Id IN :idList];
           system.debug('@@userList '+userList );
          
          for(User u : userList) 
          {
           if(u.LanguageLocaleKey=='en_US')
            {
            for(Lead leadown : Leads)
             {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
                List<String> sendTo = new List<String>();
                sendTo.add(u.Email);
                system.debug('@@sendTo'+sendTo);
                mail.setToAddresses(sendTo);
                system.debug('@@mail.setToAddresses'+mail);  
                mail.setSubject('Sandbox: New lead assigned to you:' + leadown.Name + 'From' + leadown.Company);
               
                String body = '*** NEW LEAD ASSIGNMENT NOTIFICATION ***';
                body += 'The following new Lead has been assigned to you:';
                body += 'Company :'+ leadown.Company;
                body += 'Lead Name : ' + leadown.Name;
                body += 'Country : '+ leadown.Country;
                body += 'Actions :'+ leadown.Recent_Actions__c;
                body += 'Lead Source - Most Recent :' + leadown.LeadSource;
                body += 'Lead Status : ' + leadown.Status;

                body += 'Click on the link to access the lead directly :'+ URL.getSalesforceBaseUrl().toExternalForm() + '/' + leadown.Name;
                mail.setHtmlBody(body); 
                emails.add(mail);
                system.debug('@@emails'+emails);
               
             }
            
            }
          
          }
    
        Messaging.sendEmail(emails);
      }
     }
    }
}


Below is the trigger on after update:

 if(Trigger.isAfter)
       {
        if(Trigger.isupdate)
        { 
          //if(RecursiveTriggerHandler.isFirstTime){
          //RecursiveTriggerHandler.isFirstTime = false;
           
           HelperLeadTrigger.sendEmail(trigger.new);
          // }
        }
      }

 
The new LearnerThe new Learner
i have one more question here, as of now i am only considering the users under queue, how can i send an email public group users under queue in salesforce