Trigger to Post Automated Random Chatter Comment

Requirement: Write a trigger to post an automated random comment whenever a chatter post is published.
Use Case: When any user posts to chatter and if the user’s post contains the term “Success”, an automated comment should be posted. This comment should be randomly selected from a list of comments; And should be posted as a specific user.

To achieve this requirement, we will be using two Chatter objects viz. Feed Item and Feed Comment. ‘Feed Item’ records are the posts created by a User and the ‘Feed Comment’ records are the comments on that post.

In other words, our requirement is to insert a random ‘Feed Comment’ record, whenever a ‘Feed Item’ record is inserted.

The Trigger should be written with the following flow:

  1. Define the Trigger
  2. Create a list for DML (last step)
  3. Create a List of ‘Strings’/ Comments
  4. Get a Random comment from the List in Step 3
  5. Iterate through each Feed Item record in the List from Step 2. and add the random comment.
  6. DML to insert List of newly created records

 

Now lets go through each step once again and write its respective code.

Defining the trigger (the trigger syntax)

Lets name our trigger randomChatterComment. The trigger will be on the FeedItem object and will be an After Insert trigger.

But why an after insert trigger? That’s because the requirement is to create a comment on the FeedItem record being inserted. And we will need the FeedItems’ ID to relate the Comment to it. The ID’s and other system fields are available only in an after trigger.

The code defining our trigger will be:

trigger randomChatterComment on FeedItem (after insert) {

Create a List of FeedComments for the final insert

In this step, we will create a List of FeedComments that will be used in DML (last step)

    //Create a list to hold the list of Feed Comment records being inserted
    List<FeedComment> List2insert = New list<FeedComment>();

 

Create a List of Strings/Comments

In this step, we will create a list of Strings (comments) from which a random one will be selected in the next step.

//Create a list of Strings
    List<String> commentList = New List<String>{
        'Congratulations and BRAVO!',
            'This calls for celebrating! Congratulations!',
            'You did it! So proud of you!',
            'I knew it was only a matter of time. Well done!',
            'Congratulations on your well-deserved success.',
            'Heartfelt congratulations to you.',
            'Warmest congratulations on your achievement.',
            'Congratulations and best wishes for your next adventure!',
            'So pleased to see you accomplishing great things.',
            'Feeling so much joy for you today. What an impressive achievement!',
            'You’ve worked so hard for this. Congrats!',
            'This is awesome! You’re awesome! Way to go!',
            'Here’s to your streak! Keep it up!',
            'Sincere congratulations on your hard-earned success',
            'You are proof that good things come to those who are willing to sacrifice to reach a worthwhile goal. Words can’t express how proud I am!',
            'You have the creativity and determination to do whatever you can dream. I hope you feel proud today and confident in your ability to rise to your next challenge.',
            'Celebrating the dedication you’ve shown on the way to this achievement. You’ve earned every bit of the success you’re enjoying.',
            'Congrats!!!'           
            };

Get a Random Comment

To get a random comment, we will first generate a random number. Then use the GET method to retrieve a comment from the List.

//Get a random number which is less than the size of the above list
   Integer i = (Math.random()* (commentList.size() -1)).intvalue();

Iterate through Feed Item records and add comment

In this step, we will iterate through each FeedItem record that entered the trigger. Remember, these are the records available in trigger.new

We will also use an If condition to check if the FeedItem record contains the word ‘Success’. If yes, a we will create a FeedComment record with a random comment.

We will then add all the newly created FeedComment records to a final List that will be used in DML in the next step.

    //iterate through each feed item record and add the comments
    For (FeedItem FIR:Trigger.new) { 
        //check if the post contains the word "Success"
        If (FIR.Body.contains('success')) {
            FeedComment FC = New FeedComment(); 
            FC.CommentBody = commentList.get(i);//add a random comment from the comment list using the random number generated earlier
            FC.FeedItemID = FIR.id;
            FC.CreatedById = '005f4000002NG0g'; //ID of user who will be commenting
            List2insert.add(fc); //add to final list of comments that needs to be inserted
        }
    }

 

DML to Insert the List of Feed Comments

Insert List2insert; // insert the final list of comments

 

The complete trigger to post a random chatter comment will be as follows:

trigger randomChatterComment on FeedItem (after insert) {
    
    //Create a list to hold the list of Feed Comment records being inserted
    List<FeedComment> List2insert = New list<FeedComment>();
    
    //Create a list of comments
    List<String> commentList = New List<String>{
        'Congratulations and BRAVO!',
            'This calls for celebrating! Congratulations!',
            'You did it! So proud of you!',
            'I knew it was only a matter of time. Well done!',
            'Congratulations on your well-deserved success.',
            'Heartfelt congratulations to you.',
            'Warmest congratulations on your achievement.',
            'Congratulations and best wishes for your next adventure!',
            'So pleased to see you accomplishing great things.',
            'Feeling so much joy for you today. What an impressive achievement!',
            'You’ve worked so hard for this. Congrats!',
            'This is awesome! You’re awesome! Way to go!',
            'Here’s to your streak! Keep it up!',
            'Sincere congratulations on your hard-earned success',
            'You are proof that good things come to those who are willing to sacrifice to reach a worthwhile goal. Words can’t express how proud I am!',
            'You have the creativity and determination to do whatever you can dream. I hope you feel proud today and confident in your ability to rise to your next challenge.',
            'Celebrating the dedication you’ve shown on the way to this achievement. You’ve earned every bit of the success you’re enjoying.',
            'Congrats!!!'           
            };
                //Get a random number which is less than the size of the above list
                Integer i = (Math.random()* (commentList.size() -1)).intvalue();
    
    //iterate through each feed item record and add the comments
    For (FeedItem FIR:trigger.new) { 
        //check if the post contains the word "Success"
        If (FIR.Body.contains('success')) {
            FeedComment FC = New FeedComment(); 
            FC.CommentBody = commentList.get(i);//add a random comment from the comment list using the random number generated earlier
            FC.FeedItemID = FIR.id;
            FC.CreatedById = '005f4000002NG0g'; //ID of user who will be commenting
            List2insert.add(fc); //add to final list of comments that needs to be inserted
        }
    }
    Insert List2insert; // insert the final list of comments
    
}

 


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.