<\/p>\n
Create a List of FeedComments for the final insert<\/h2>\n
In this step, we will create a List of FeedComments that will be used in DML (last step)<\/p>\n
\/\/Create a list to hold the list of Feed Comment records being inserted\r\n List<FeedComment> List2insert = New list<FeedComment>();<\/pre>\n <\/p>\n
<\/div>
<\/p>\n
Create a List of Strings\/Comments<\/h2>\n
In this step, we will create a list of Strings (comments) from which a random one will be selected in the next step.<\/p>\n
\/\/Create a list of Strings\r\n List<String> commentList = New List<String>{\r\n 'Congratulations and BRAVO!',\r\n 'This calls for celebrating! Congratulations!',\r\n 'You did it! So proud of you!',\r\n 'I knew it was only a matter of time. Well done!',\r\n 'Congratulations on your well-deserved success.',\r\n 'Heartfelt congratulations to you.',\r\n 'Warmest congratulations on your achievement.',\r\n 'Congratulations and best wishes for your next adventure!',\r\n 'So pleased to see you accomplishing great things.',\r\n 'Feeling so much joy for you today. What an impressive achievement!',\r\n 'You\u2019ve worked so hard for this. Congrats!',\r\n 'This is awesome! You\u2019re awesome! Way to go!',\r\n 'Here\u2019s to your streak! Keep it up!',\r\n 'Sincere congratulations on your hard-earned success',\r\n 'You are proof that good things come to those who are willing to sacrifice to reach a worthwhile goal. Words can\u2019t express how proud I am!',\r\n '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.',\r\n 'Celebrating the dedication you\u2019ve shown on the way to this achievement. You\u2019ve earned every bit of the success you\u2019re enjoying.',\r\n 'Congrats!!!' \r\n };<\/pre>\n<\/div>
<\/p>\n
Get a Random Comment<\/h2>\n
To get a random comment, we will first generate a random number. Then use the GET method to retrieve a comment from the List.<\/p>\n
\/\/Get a random number which is less than the size of the above list\r\n Integer i = (Math.random()* (commentList.size() -1)).intvalue();<\/pre>\n<\/div>
<\/p>\n
Iterate through Feed Item records and add comment<\/h2>\n
In this step, we will iterate through each FeedItem record that entered the trigger. Remember, these are the records available in trigger.new<\/p>\n
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.<\/p>\n
We will then add all the newly created FeedComment records to a final List that will be used in DML in the next step.<\/p>\n
\/\/iterate through each feed item record and add the comments\r\n For (FeedItem FIR:Trigger.new) { \r\n \/\/check if the post contains the word \"Success\"\r\n If (FIR.Body.contains('success')) {\r\n FeedComment FC = New FeedComment(); \r\n FC.CommentBody = commentList.get(i);\/\/add a random comment from the comment list using the random number generated earlier\r\n FC.FeedItemID = FIR.id;\r\n FC.CreatedById = '005f4000002NG0g'; \/\/ID of user who will be commenting\r\n List2insert.add(fc); \/\/add to final list of comments that needs to be inserted\r\n }\r\n }<\/pre>\n <\/p>\n
<\/div>
<\/p>\n
DML to Insert the List of Feed Comments<\/h2>\nInsert List2insert; \/\/ insert the final list of comments<\/pre>\n<\/div><\/div><\/div>
<\/div>\n
<\/p>\n
The complete trigger to post a random chatter comment will be as follows:<\/strong><\/p>\ntrigger randomChatterComment on FeedItem (after insert) {\r\n \r\n \/\/Create a list to hold the list of Feed Comment records being inserted\r\n List<FeedComment> List2insert = New list<FeedComment>();\r\n \r\n \/\/Create a list of comments\r\n List<String> commentList = New List<String>{\r\n 'Congratulations and BRAVO!',\r\n 'This calls for celebrating! Congratulations!',\r\n 'You did it! So proud of you!',\r\n 'I knew it was only a matter of time. Well done!',\r\n 'Congratulations on your well-deserved success.',\r\n 'Heartfelt congratulations to you.',\r\n 'Warmest congratulations on your achievement.',\r\n 'Congratulations and best wishes for your next adventure!',\r\n 'So pleased to see you accomplishing great things.',\r\n 'Feeling so much joy for you today. What an impressive achievement!',\r\n 'You\u2019ve worked so hard for this. Congrats!',\r\n 'This is awesome! You\u2019re awesome! Way to go!',\r\n 'Here\u2019s to your streak! Keep it up!',\r\n 'Sincere congratulations on your hard-earned success',\r\n 'You are proof that good things come to those who are willing to sacrifice to reach a worthwhile goal. Words can\u2019t express how proud I am!',\r\n '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.',\r\n 'Celebrating the dedication you\u2019ve shown on the way to this achievement. You\u2019ve earned every bit of the success you\u2019re enjoying.',\r\n 'Congrats!!!' \r\n };\r\n \/\/Get a random number which is less than the size of the above list\r\n Integer i = (Math.random()* (commentList.size() -1)).intvalue();\r\n \r\n \/\/iterate through each feed item record and add the comments\r\n For (FeedItem FIR:trigger.new) { \r\n \/\/check if the post contains the word \"Success\"\r\n If (FIR.Body.contains('success')) {\r\n FeedComment FC = New FeedComment(); \r\n FC.CommentBody = commentList.get(i);\/\/add a random comment from the comment list using the random number generated earlier\r\n FC.FeedItemID = FIR.id;\r\n FC.CreatedById = '005f4000002NG0g'; \/\/ID of user who will be commenting\r\n List2insert.add(fc); \/\/add to final list of comments that needs to be inserted\r\n }\r\n }\r\n Insert List2insert; \/\/ insert the final list of comments\r\n \r\n}<\/pre>\n <\/p>\n","protected":false},"excerpt":{"rendered":"
Apex Trigger to post an automated random comment whenever a chatter post is published. 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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[746],"tags":[755,787,788,791,790,789,770,777],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-salesforce-apex-triggers","tag-apex","tag-chatter","tag-comment","tag-feedcomment","tag-feeditem","tag-post","tag-salesforce","tag-trigger"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/comments?post=1099"}],"version-history":[{"count":0,"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/posts\/1099\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/media?parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/categories?post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.syedtaha.com\/wp-json\/wp\/v2\/tags?post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}