<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>chatter | Taha Syed | Salesforce</title>
<atom:link href="https://www.syedtaha.com/tag/chatter/feed/" rel="self" type="application/rss+xml" /><link>https://www.syedtaha.com</link>
<description>Taha Syed &#124; Sales to Salesforce &#124; Dissecting and Defining the Rationale..</description>
<lastBuildDate>Mon, 17 Dec 2018 20:19:16 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>
hourly	</sy:updatePeriod>
<sy:updateFrequency>
1	</sy:updateFrequency>
<generator>https://wordpress.org/?v=6.9.4</generator>
<item><title>Trigger to Post Automated Random Chatter Comment</title><link>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Mon, 03 Dec 2018 20:45:03 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[apex]]></category>
<category><![CDATA[chatter]]></category>
<category><![CDATA[comment]]></category>
<category><![CDATA[feedcomment]]></category>
<category><![CDATA[feeditem]]></category>
<category><![CDATA[post]]></category>
<category><![CDATA[salesforce]]></category>
<category><![CDATA[trigger]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=1099</guid><description><![CDATA[<p>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>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/">Trigger to Post Automated Random Chatter Comment</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<p><strong>Requirement: </strong>Write a trigger to post an automated random comment whenever a chatter post is published.<br
/>
<strong>Use Case:</strong> When any user posts to chatter and if the user&#8217;s post contains the term &#8220;Success&#8221;, 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><p>To achieve this requirement, we will be using two Chatter objects viz. <strong>Feed Item</strong> and <strong>Feed Comment. &#8216;</strong>Feed Item&#8217; records are the posts created by a User and the &#8216;Feed Comment&#8217; records are the comments on that post.</p><p>In other words, our requirement is to <strong>insert a random &#8216;Feed Comment&#8217; record, whenever a &#8216;Feed Item&#8217; record is inserted.</strong></p><p>The Trigger should be written with the following flow:</p><ol><li>Define the Trigger</li><li>Create a list for DML (last step)</li><li>Create a List of &#8216;Strings&#8217;/ Comments</li><li>Get a Random comment from the List in Step 3</li><li>Iterate through each Feed Item record in the List from Step 2. and add the random comment.</li><li>DML to insert List of newly created records</li></ol><p>&nbsp;</p><p>Now lets go through each step once again and write its respective code.</p><div
class="tab_widget wp_shortcodes_tabs"><ul
class="wps_tabs"><li><a
href="#" data-tab="tab-0-step-1">Step 1</a></li><li><a
href="#" data-tab="tab-1-step-2">Step 2</a></li><li><a
href="#" data-tab="tab-2-step-3">Step 3</a></li><li><a
href="#" data-tab="tab-3-step-4">Step 4</a></li><li><a
href="#" data-tab="tab-4-step-5">Step 5</a></li><li><a
href="#" data-tab="tab-5-step-6">Step 6</a></li></ul><div
class="tab_container"><div
id="tab-0-step-1" class="tab_content clearfix"></p><h2>Defining the trigger (the trigger syntax)</h2><p>Lets name our trigger <strong>randomChatterComment. </strong>The trigger will be on the <strong>FeedItem </strong>object and will be an <strong>After Insert</strong> trigger.</p><p>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 <strong>after</strong> trigger.</p><p>The code defining our trigger will be:</p><pre class="lang:default decode:true" title="Step 1">trigger randomChatterComment on FeedItem (after insert) {</pre><p></div><div
id="tab-1-step-2" class="tab_content clearfix"></p><h2>Create a List of FeedComments for the final insert</h2><p>In this step, we will create a List of FeedComments that will be used in DML (last step)</p><pre class="lang:default decode:true ">    //Create a list to hold the list of Feed Comment records being inserted
    List&lt;FeedComment&gt; List2insert = New list&lt;FeedComment&gt;();</pre><p>&nbsp;</p><p></div><div
id="tab-2-step-3" class="tab_content clearfix"></p><h2>Create a List of Strings/Comments</h2><p>In this step, we will create a list of Strings (comments) from which a random one will be selected in the next step.</p><pre class="lang:default decode:true ">//Create a list of Strings
    List&lt;String&gt; commentList = New List&lt;String&gt;{
        '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!!!'           
            };</pre><p></div><div
id="tab-3-step-4" class="tab_content clearfix"></p><h2>Get a Random Comment</h2><p>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><pre class="lang:default decode:true ">//Get a random number which is less than the size of the above list
   Integer i = (Math.random()* (commentList.size() -1)).intvalue();</pre><p></div><div
id="tab-4-step-5" class="tab_content clearfix"></p><h2>Iterate through Feed Item records and add comment</h2><p>In this step, we will iterate through each FeedItem record that entered the trigger. Remember, these are the records available in trigger.new</p><p>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><p>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><pre class="lang:default decode:true">    //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
        }
    }</pre><p>&nbsp;</p><p></div><div
id="tab-5-step-6" class="tab_content clearfix"></p><h2>DML to Insert the List of Feed Comments</h2><pre class="lang:default decode:true">Insert List2insert; // insert the final list of comments</pre><p></div></div></div><div
class="clear"></div><p>&nbsp;</p><p><strong>The complete trigger to post a random chatter comment will be as follows:</strong></p><pre class="lang:default decode:true ">trigger randomChatterComment on FeedItem (after insert) {
    
    //Create a list to hold the list of Feed Comment records being inserted
    List&lt;FeedComment&gt; List2insert = New list&lt;FeedComment&gt;();
    
    //Create a list of comments
    List&lt;String&gt; commentList = New List&lt;String&gt;{
        '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
    
}</pre><p>&nbsp;</p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/">Trigger to Post Automated Random Chatter Comment</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></content:encoded>
<wfw:commentRss>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
</channel>
</rss>