Skip to content

Instantly share code, notes, and snippets.

@conrjac
Created June 25, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conrjac/2c3d4485cf3579a839bae09373b3ab42 to your computer and use it in GitHub Desktop.
Save conrjac/2c3d4485cf3579a839bae09373b3ab42 to your computer and use it in GitHub Desktop.
IdeaCommentTriggerHandler
public class IdeaCommentTriggerHandler {
public static void handleTrigger(List<IdeaComment> workingRecords, List<IdeaComment> oldRecords, System.TriggerOperation triggerEvent ) {
final Id orgWideAddress = [select Id from OrgWideEmailAddress WHERE Address ='salesforce@your.org'].Id; // You should consider using a custom setting to manage this - certainly do not hardcode an address Id!
switch on triggerEvent {
when AFTER_INSERT, AFTER_UPDATE{
// For the comments in trigger context, get the related ideas - store in map to easily pull values later in email message.
Set<Id> ideasCommented = new Set<Id>();
for(IdeaComment ic : workingRecords) ideasCommented.add(ic.IdeaId);
Map<Id, Idea> ideas = new Map<Id, Idea>([SELECT Id,CreatedById,LastCommentId, Title FROM Idea WHERE Id IN :ideasCommented]);
// Create a collection to store a map of IdeaId and the last comment text, so that this be shared in the email message.
Set<Id> lastCommentIds = new Set<Id>();
For(Idea i : ideas.values()) lastCommentIds.add(i.LastCommentId);
Map<Id, String> lastComments = new Map<Id, String>();
For(IdeaComment ic :workingRecords) lastComments.put(ic.IdeaId, ic.CommentBody);
// Get all of the previous comment history for the ideas relating to the comments in trigger context, this is so we can email previous posters of new commnets.
List<IdeaComment> comments = [SELECT Id, IdeaId, CreatedById, CommentBody FROM IdeaComment WHERE IdeaId IN :ideasCommented];
// Create a unique set of ideas, and then using the comment history, a list of all the people that have commented on that idea.
Map<Id, Set<Id>> ideaCommentors = New Map<Id, Set<Id>>();
for(IdeaComment comment : comments)
{
if (ideaCommentors.containsKey(comment.IdeaId)) {
ideaCommentors.get(comment.IdeaId).add(comment.createdById);
}
else {
ideaCommentors.put(comment.IdeaId, new Set<Id>{ comment.createdById });
}
}
// Add the original idea creators to the list of people we will send email notifications too.
for(Idea i : ideas.values())
{
if (ideaCommentors.containsKey(i.Id)) {
ideaCommentors.get(i.Id).add(i.createdById);
}
else {
ideaCommentors.put(i.Id, new Set<Id>{ i.createdById });
}
}
// For all of the ideas, that we have comments for in the trigger context, send email alerts to everyone that has commented in the past, and the original Idea creator.
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(Idea i : ideas.values())
{
String subject = 'New Idea Comment - ' + i.Title;
String fullRecordURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + i.Id;
String body = '<p>Hello, a new comment has been posted an Idea that you have created, or previously commented on. </p>';
body += '<p><strong>Most Recent Comment: <em>(Images Removed)</em></strong></p>';
body += '<blockquote>' + lastComments.get(i.Id) + '</blockquote>';
body += '<p><strong>You can reply, to the comment by visiting the Idea Record - ' + fullRecordURL + '</strong></p>';
// Create a master list to store the emails that we will send
for(Id userId : ideaCommentors.get(i.Id))
{
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// If the org wide address quried at the start of this class is NULL, then the setOrgWideEmailAddressId is not called(set) and the email will be sent as the user who is running the trigger context.
if(orgWideAddress != null) email.setOrgWideEmailAddressId(orgWideAddress);
email.setTargetObjectId(userId);
email.setUseSignature(false);
// Sets the paramaters of the email
email.setSubject( subject );
email.setHtmlBody( body );
email.setSaveAsActivity(false);
// Add your email address into the master list
emails.add(email);
}
}
// Sends mass emails
Messaging.sendEmail(emails);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment