Skip to content

Instantly share code, notes, and snippets.

@tfuda
Last active January 17, 2019 18:38
Show Gist options
  • Save tfuda/3ccddc3e5855aba34b3f738477d7002f to your computer and use it in GitHub Desktop.
Save tfuda/3ccddc3e5855aba34b3f738477d7002f to your computer and use it in GitHub Desktop.
private static void sendUsernameChangeEmails(Set<Id> userIdSet) {
if (userIdSet.isEmpty()) return;
EmailTemplate template = OrderProcessingUtils.getEmailTemplate('Patron_Ticket_Patron_Portal_New_Username');
PatronTicketSettings__c settings = TicketUtil.getSettings();
OrgWideEmailAddress owea = null;
String senderDisplayName = null;
// See if we have an org wide email address and use that if so
if (settings.FromEmailAddress__c != null) {
List<OrgWideEmailAddress> aList = [SELECT Id, DisplayName FROM OrgWideEmailAddress WHERE Id = :settings.FromEmailAddress__c LIMIT 1];
if(!aList.isEmpty()){
owea = aList[0];
}
}
//if we weren't able to set to the org wide set to orderfulfillmentadmin
if (settings.OrderFulfillmentAdministrator__c != null) {
List<User> uList = [SELECT Id, Name, Email FROM User WHERE Id = :settings.OrderFulfillmentAdministrator__c LIMIT 1];
if (!uList.isEmpty()) {
senderDisplayName = uList[0].name;
}
}
List<Messaging.SingleEmailMessage> emailMessages = new List<Messaging.SingleEmailMessage>();
for (Id userId : userIdSet) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
OrderProcessingUtils.determineEmailFrom(email);
email.setTemplateId(template.Id);
email.setTargetObjectId(userId);
if (owea != null) {
email.setOrgWideEmailAddress(owea.Id);
} else if (String.isNotBlank(senderDisplayName)) {
email.setSenderDisplayName(senderDisplayName);
}
email.setUseSignature(false);
emailMessages.add(email);
}
// This doesn't raise an exception, it returns a Messaging.SendEmailResult array.
// Since we're ignoring failures to send the Username change emails, we don't need any special exception handling here.
Messaging.sendEmail(emailMessages);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment