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:
- Define the Trigger
- Create a list for DML (last step)
- Create a List of ‘Strings’/ Comments
- Get a Random comment from the List in Step 3
- Iterate through each Feed Item record in the List from Step 2. and add the random comment.
- DML to insert List of newly created records
Now lets go through each step once again and write its respective code.
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 }
Leave a Reply