+ Start a Discussion
Wim van BeekWim van Beek 

Send private message to user via chatter

I am looking for a solution to send a personal message to one specific user.

Now i use in a trigger
<cut some lines here>
ConnectApi.FeedItem feedItemRep = ConnectApi.ChatterFeeds.postFeedItem(Null, ConnectApi.FeedType.UserProfile, UserId, BinSegment);

and the message is forwarded to the user as indicated in UserId.

BUT

If an user starts following another user, he/she gets all messages from the other.

How can i force the message to be send to that one user only !

Best Answer chosen by Wim van Beek
James LoghryJames Loghry
You made me really dig for this one!  

What you want is ChatterMessages / Private Messages instead.  Anything else will be visibile on that user's feed and essentially is public.

Take a look at the sendMessage method here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ConnectAPI_ChatterMessages_static_methods.htm#apex_ConnectAPI_ChatterMessages_sendMessage_2

Alternatively, you could use the REST Api.  The following is a document for the Connect REST API (behaves the same).  Do a search for "Send a Private Message" and you'll find an example there.
http://www.salesforce.com/us/developer/docs/chatterapi/salesforce_chatter_rest_api.pdf


User-added image


For instance, in the screenshot, I am testing the Connect REST API and private message functionality in workbench.  (If you're not familiar with workbench, log into workbench.developerforce.com with your Salesforce credentials and poke around).

All Answers

James LoghryJames Loghry
You made me really dig for this one!  

What you want is ChatterMessages / Private Messages instead.  Anything else will be visibile on that user's feed and essentially is public.

Take a look at the sendMessage method here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ConnectAPI_ChatterMessages_static_methods.htm#apex_ConnectAPI_ChatterMessages_sendMessage_2

Alternatively, you could use the REST Api.  The following is a document for the Connect REST API (behaves the same).  Do a search for "Send a Private Message" and you'll find an example there.
http://www.salesforce.com/us/developer/docs/chatterapi/salesforce_chatter_rest_api.pdf


User-added image


For instance, in the screenshot, I am testing the Connect REST API and private message functionality in workbench.  (If you're not familiar with workbench, log into workbench.developerforce.com with your Salesforce credentials and poke around).
This was selected as the best answer
Wim van BeekWim van Beek
Thanks for digging !

ConnectAPI.ChatterMessage SendMsg = ConnectAPI.ChatterMessages.sendMessage('Some Text', UserId);
system.debug('SendMsg '+SendMsg);

This is the trick for me. Somehow I got lost in the documentation.