Salesforce Chatter Triggers

Today I wanted to create an app that would allow me to add my activity so that I can analyse my time at a future date. I started my trusty IDE to start mocking up the idea when I had to step back for a moment and think there must be a simpler way to do this.

I use Salesforce for managing my projects and I usually post chatter messages to myself and others. What if I could post a custom # message that would be intercepted and posted to a custom object.

No problem… I created a custom object called Activity:

image

Then all you need to do is go to Customize > Chatter > Triggers and add a new trigger on the Feed Item.It’s then a matter of looking for a #activity tag in the feed item body, pulling the entry out and inserting it into my Activity custom object.

<span class="lnum">   1:  </span>trigger ActivityPost on FeedItem (after insert) {
<span class="lnum">   2:  </span>    <span class="kwrd">for</span> (FeedItem f : trigger.<span class="kwrd">new</span>)
<span class="lnum">   3:  </span>    {
<span class="lnum">   4:  </span>        <span class="kwrd">if</span> (f.Body.startsWith(<span class="str">'#activity'</span>)) {
<span class="lnum">   5:  </span>            funcoder__Activity__c a = <span class="kwrd">new</span> 
                                 funcoder__Activity__c (
<span class="lnum">   6:  </span>                Name = f.Body.replace(<span class="str">'#activity'</span>,<span class="str">''</span>)
<span class="lnum">   7:  </span>            );
<span class="lnum">   8:  </span>
<span class="lnum">   9:  </span>            insert a;
<span class="lnum">  10:  </span>        }
<span class="lnum">  11:  </span>    }
<span class="lnum">  12:  </span>}

Now just go and enter a chatter message, for example #activity Developed an activity application in Salesforce.

5 thoughts on “Salesforce Chatter Triggers

  1. Jonathan,

    Thank you for your contribution. I have read a lot of your articles.

    I am very new to triggers and got this error when I try to create a trigger on the FeedItem:

    Error: Compile Error: Invalid type: funcoder__Activity__c at line 6 column 32

    Do I need to create something called funcoder__Activity__c before this will work.

    Sorry if this is a painfully obvious question.

    John

    1. Hi John

      I created an object called Activity with fields to hold the information. My alias is funcoder in my account but you will find that yours is different, so the object you are building with. Just go to Setup / App Setup / Create / Objects then click on the object you created (mine was Activity). In the details section you will see API Name. That’s the name you need to use in your Apex code.

      All the best

      Jonathan

  2. Hi Jonathan,

    Awesome!!

    Thank you so much for your help.

    It is greatly appreciated!

    May the Force(.com) be with you!!

    John

  3. Hi Jonathan,

    I want to restrict access of few platform License user to all Chatter Feeds, All groups and documents. Do you have any solution.

Leave a Reply

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