Notifications
Clear all
Aug 13, 2021 8:24 am
What would be the simplest way to send emails alerts to Salesforce system admins from apex?
1 Reply
Aug 13, 2021 8:28 am
Try this:
List<User> userList = new List<User>(); userList = [SELECT Email FROM User WHERE Profile.Name = 'System Administrator' AND IsActive = True] ; List<String> toAddresses = new List<String>(); for (User u : userList) { toAddresses.add(u.Email); } // System.debug('toAddresses: ' + toAddresses); String messageBody = 'message body here '; Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.toAddresses = toAddresses; message.subject = 'subject here'; message.plainTextBody = messageBody; Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message}; Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); if (results[0].success) { System.debug('The email was sent successfully.'); } else { System.debug('The email failed to send: ' + results[0].errors[0].message); }