Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the broken-link-checker domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home1/syedtah1/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the updraftplus domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home1/syedtah1/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the advanced-ads domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home1/syedtah1/public_html/wp-includes/functions.php on line 6114

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/syedtah1/public_html/wp-includes/functions.php:6114) in /home1/syedtah1/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":1099,"date":"2018-12-03T20:45:03","date_gmt":"2018-12-03T20:45:03","guid":{"rendered":"http:\/\/www.syedtaha.com\/?p=1099"},"modified":"2018-12-17T20:19:16","modified_gmt":"2018-12-17T20:19:16","slug":"trigger-to-post-automated-random-chatter-comment","status":"publish","type":"post","link":"https:\/\/www.syedtaha.com\/salesforce-apex-triggers\/trigger-to-post-automated-random-chatter-comment\/1099\/","title":{"rendered":"Trigger to Post Automated Random Chatter Comment"},"content":{"rendered":"

Requirement: <\/strong>Write a trigger to post an automated random comment whenever a chatter post is published.
\nUse Case:<\/strong> 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

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

In other words, our requirement is to insert a random ‘Feed Comment’ record, whenever a ‘Feed Item’ record is inserted.<\/strong><\/p>\n

The Trigger should be written with the following flow:<\/p>\n

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

     <\/p>\n

    Now lets go through each step once again and write its respective code.<\/p>\n