5

I have tried this to send mail to the logged in user

String[] toAddresses = new String[] {UserInfo.getUserEmail()};
mail.setToAddresses(toAddresses);

This works fine for the users who have logged in, but I would like to send mail only to System Administrator.

Thanks in advance.

4
  • You mean send mail only if the user is System Administrator? Commented Jan 20, 2016 at 8:29
  • Whoever logs in I want the mail to be send mail to the system admin irrespective of whoever the user be.@SantanuHalder Commented Jan 20, 2016 at 8:33
  • 1
    than query users with System Admin profile and use their email. like - Profile p = [SELECT id FROM Profile WHERE name = 'System Administrator']; Users[] u_to_send = [SELECT Id, Email FROM User WHERE ProfileId = :p.Id]; String[] toAddresses = new String[] {u_to_send.get(0).Email}; mail.setToAddresses(toAddresses); but I would recommend to use setTargetid of user to avoid limit of sending direct emails to addresses
    – kurunve
    Commented Jan 20, 2016 at 8:42
  • it worked fine @kurunve. Thanks. Can you post this as answer Commented Jan 20, 2016 at 9:07

3 Answers 3

6

Basic idea would be to query user that you need (f.e. with system admin profile etc), and use email on that field. For example:

Profile sys_adm_profile = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
List<User> sys_adm = [SELECT id, Email FROM User WHERE ProfileId = :sys_adm_profile];
String[] toAddresses = new String[] {sys_adm.Email};
mail.setToAddresses(toAddresses);

There is better approach -- as well as email is being send to user, then mailsetTargetObjectId(userId) should be used to avoid limits of email that can be send for different email addresses. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm#email_limits_section

Also those method of getting system admin profile is not perfect -- it would fail in orgs with non english language.

5

You can do it with just one query:

SELECT Email FROM User WHERE Profile.Name = 'System Administrator'
0

//if person select other gender then send email to system adiminstrator public class personClassHandler {

public static void sendEmailongenderselection(List<Person__c>newList,List<Person__c>oldList){
    
    List<Messaging.SingleEmailMessage>mails = new List<Messaging.SingleEmailMessage>();
  
    
    if(trigger.isAfter &&(trigger.isInsert||trigger.isUpdate)){
        for(Person__c p:newList){
            if(p.Gender__c=='Others'){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
                List<String>toAddresses = new List<String>{};   
            
   List<User>u = [SELECT Email FROM User WHERE Profile.Name = 'System Administrator'];
                for(user ui:u){
                  toAddresses.add(ui.Email);
                }
                
          
          mail.setToAddresses(toAddresses);
                String body = 'Some one has selected others gender';
                mail.setHtmlBody(body);
                mails.add(mail);
                System.debug('mails'+mails);
            }
            messaging.sendEmail(mails);
            
        }
    }
    
}

}

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 9, 2022 at 5:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .