Sending of Email to Idea creator on new comment being posted

For some of our Service cloud customers we have been requested the following requirements.

When a user posts a comment on Idea, the original user that posted the idea does not receive the comments posted via email. The original poster of the idea needs to go to the Idea to read these comments.

This article explains how to send an email notification when someone commented on your Idea in Salesforce instance. This can be achieved through apex trigger. This trigger will work using standard Idea and IdeaComments objects.

Follow given steps:

  1. Click on setup button>

    Sending Email to Idea Creator on new Comment Image1

  2. Type “Idea” in left side bar search box. Click on Trigger as followed by below figure.

    Sending Email to Idea Creator on new Comment Image2

  3. Click on “New” button.

    Sending Email to Idea Creator on new Comment Image3

  4. Paste below given apex trigger code. The trigger gets invoked when a new comment is posted. It finds out the user who posted the Idea, and informs the user that a new comment has been posted and provides details of the comment.
    trigger emailOnComment on IdeaComment (after insert){
       List <Messaging.SingleEmailMessage> s=new List <Messaging.SingleEmailMessage>();
       //Create map to get Idea ID
       Map<id,Idea> mp=new Map<id,Idea>([select id,createdByid from Idea]);
       //Create map to get all User email address
       Map<id,User> mp1=new Map<id,User>([select id,Email from User]);
       for(IdeaComment o : Trigger.new){
          // get email address of Idea creator
          String userEmail = mp1.get( mp.get(o.Ideaid).createdByid ).Email;
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[] {userEmail};
          mail.setToAddresses (toAddresses);
          // Set email subject
          mail.setSubject('Automated email: Comment created');
          // set comment body
          mail.setPlainTextBody('Hi, new comment is posted, description:'+o.CommentBody);
          s.add(mail);
       }
       Messaging.sendEmail(s);
    }
  5. Save trigger.
  6. Develop a test class

    Test Class :

    @isTest
    public class emailOnCommentTest{
         static testMethod void CreateComment(){
         List Community> comobj;
         
        comobj =new List<Community>([select Id from Community limit 1]);
        
         for (Community com1:comobj){
     Idea ideaobj=new Idea();
         ideaobj.Body='Test idea';
         ideaobj.Title='test title';
         ideaobj.CommunityId=com1.ID;
         insert ideaobj;
         
         IdeaComment ic =new IdeaComment();
         ic.CommentBody='Hi Test';
         //ic.CreatorName='Test123';
         ic.IdeaId=ideaobj.ID;
         insert ic;
          }
        }
    }
    )

For any query on Sending Of Email To Idea Creator On New Comment Being Posted, contact support@astreait.com