+ Start a Discussion
Ben Olsen 12Ben 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);
    
}
 
Best Answer chosen by Ben Olsen 12
Sumitkumar_ShingaviSumitkumar_Shingavi
Hello Ben,

I cleaned your code and you can try below:
trigger IdeaTrigger on Idea (after update) {
    
    Set<Id> ideaIds = new Set<Id>();
	
	for(Idea ideaInstance : Trigger.new) {
		if(ideaInstance.Idea_Status__c != Trigger.oldMap.get(ideaInstance.Id).Idea_Status__c) {
			ideaIds.add(ideaInstance.Id);
		}
	}
    
    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 : [SELECt Id, Name FROM Idea WHERE Id in :ideaIds]){
        
        //"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);    
}
Hope this helps!
 

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
Hello Ben,

I cleaned your code and you can try below:
trigger IdeaTrigger on Idea (after update) {
    
    Set<Id> ideaIds = new Set<Id>();
	
	for(Idea ideaInstance : Trigger.new) {
		if(ideaInstance.Idea_Status__c != Trigger.oldMap.get(ideaInstance.Id).Idea_Status__c) {
			ideaIds.add(ideaInstance.Id);
		}
	}
    
    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 : [SELECt Id, Name FROM Idea WHERE Id in :ideaIds]){
        
        //"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);    
}
Hope this helps!
 
This was selected as the best answer
Ben Olsen 12Ben Olsen 12
Wow this is great, what would I need to change if i wanted to use an email template we already have?
Sumitkumar_ShingaviSumitkumar_Shingavi
You can use email.setTemplateId(); Also select best answer if you good :)