You need to sign in to do that
Don't have an account?
Ben Olsen 12
trigger on ideas object
Hi, I would like to create a trigger on the ideas object to send an email to all voters/commentors on the particular ID, when a custom field (Idea_Status__c) is updated, Any help would be awesome, Here is what I have so far
trigger IdeaTrigger on Idea (after update) {
Set<Id> ideaIds = trigger.newMap.keySet();
Map<Id,List<User>> ideaUserMap = new Map<Id,List<User>>();
for(Id i : ideaIds)
ideaUserMap.put(i.Id,new List<User>());
for(IdeaComment ic : [SELECT Id, CreatedBy.Id,IdeaId FROM IdeaComment WHERE IdeaId IN :ideaIds]){
ideaUserMap.get(ic.IdeaId).add(ic.CreatedBy);
}
for(Vote iv : [SELECT Id, CreatedBy.Id, ParentId FROM Vote WHERE ParentId IN :ideaIds AND Type = 'Up']){
ideaUserMap.get(iv.ParentId).add(ic.CreatedBy);
}
//create list of outbound emails
Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage[]{};
for(Idea i : Trigger.new){
//Need to check that the custom field has changed on the ideas object
if(i.Idea_Status__c == oldMap.get(i).Idea_Status__c)
continue;
//"merge" fields into template
String EmailSubject = i.Name;
String EmailBody = 'Name:' + i.Name;
emailBody += '\nIdea Id: ' + i.Id;
//add the rest of the fields you want in the body.
//for each user, create a template, set the text
for(User u : ideaUserMap.get(i.Id)){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTargetObjectId(u.id);
//email.setOrgWideEmailAddressId(BlackHoleOrgWide);
email.setSubject(EmailSubject);
email.setHtmlBody(EmailBody);
messages.add(email);
}
}
//send the list of messages
Messaging.sendEmail(messages);
}
trigger IdeaTrigger on Idea (after update) {
Set<Id> ideaIds = trigger.newMap.keySet();
Map<Id,List<User>> ideaUserMap = new Map<Id,List<User>>();
for(Id i : ideaIds)
ideaUserMap.put(i.Id,new List<User>());
for(IdeaComment ic : [SELECT Id, CreatedBy.Id,IdeaId FROM IdeaComment WHERE IdeaId IN :ideaIds]){
ideaUserMap.get(ic.IdeaId).add(ic.CreatedBy);
}
for(Vote iv : [SELECT Id, CreatedBy.Id, ParentId FROM Vote WHERE ParentId IN :ideaIds AND Type = 'Up']){
ideaUserMap.get(iv.ParentId).add(ic.CreatedBy);
}
//create list of outbound emails
Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage[]{};
for(Idea i : Trigger.new){
//Need to check that the custom field has changed on the ideas object
if(i.Idea_Status__c == oldMap.get(i).Idea_Status__c)
continue;
//"merge" fields into template
String EmailSubject = i.Name;
String EmailBody = 'Name:' + i.Name;
emailBody += '\nIdea Id: ' + i.Id;
//add the rest of the fields you want in the body.
//for each user, create a template, set the text
for(User u : ideaUserMap.get(i.Id)){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTargetObjectId(u.id);
//email.setOrgWideEmailAddressId(BlackHoleOrgWide);
email.setSubject(EmailSubject);
email.setHtmlBody(EmailBody);
messages.add(email);
}
}
//send the list of messages
Messaging.sendEmail(messages);
}
I cleaned your code and you can try below: Hope this helps!
All Answers
I cleaned your code and you can try below: Hope this helps!