Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Posting a Chatter Mention via Apex

Posting a Chatter Mention via Apex

Post by Ritesh Aswaney  (2012-11-29)

Status: Unverified
Level: intermediate

How to post a Chatter @mention via APEX

There is often a need to programmatically post a Chatter mention. A classic example would be a pat on the back chatter mention for a star sales performer who has just closed a big deal. You can make use of the handy REST API to make post that Chatter mention. Dont let the rigour of getting an OAuth token deter you from reading on, we've got a much simpler solution.

Solution

The code snippet below is ideally housed in a with sharing class, so that it executes as a logged in user. First off we construct the URL, which is composed of the initial org prefix, which we obtain using the System utility method. We append to this the chatter feed URL pertaining to the record that we want to post the chatter post on. We construct a HttpRequest and initialise it as shown. Remember how I promised you won't have to do any OAuth, obeserve how the SessionId of the logged in user can be used as the OAuth token. We then handcraft the JSON request to set as the body of the Chatter post. The UserId is the Id of the User we are mentioning in the Chatter post. We are all set then, it's just about making the Http Request!

 String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
        
String url =  salesforceHost + '/services/data/v26.0/chatter/feeds/record/' + RecordId + '/feed-items';
            
HttpRequest req = new HttpRequest();
            
req.setMethod('POST');
req.setEndpoint(url);
req.setHeader('Content-type', 'application/json');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
            
req.setBody('{ "body" : { "messageSegments" : [ { "type": "mention", "id" : "' + UserId + '" }, { "type": "text",  "text" : "' + ' ' + ChatterPostText +  '" } ] } }');
Http http = new Http();
  
HTTPResponse res = http.send(req);
 

Discussion

This quick, easy and tasty recipe assumes that

  • You're trying to mention someone in a chatter post
  • It is executing as a logged in user, because we need that SessionId for it to work

References

Here are some resources which could make for interesting alongside reading

  • Getting started with understanding mentions using the Chatter REST API http://www.salesforce.com/us/developer/docs/chatterapipre/Content/connect_resources_how_to.htm#cc_post_mentions
  • How I found out that the SessionId can double up as the OAuth token http://blogs.developerforce.com/developer-relations/2011/03/accessing-chatter-user-pics.html
  • You can also use Connect in Apex (available in all DE orgs in Spring ’13) to post a Chatter mention natively in Apex: http://blogs.developerforce.com/developer-relations/2013/02/getting-started-with-connect-in-apex.html

Share

Recipe Activity - Please Log in to write a comment

You can use Connect in Apex to post Chatter mentions. See Samantha Ready's post on the Developer Force blog: http://blogs.developerforce.com/developer-relations/2013/02/getting-started-with-connect-in-apex.html

by Jody Bleyle  (2013-02-07)

Can this be used to mention more than one user in the same post? If so, is it a simple change? Thanks!

by Jason Ackerman  (2013-01-17)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.