function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chamil MadusankaChamil Madusanka 

Send Chatter private messages in Apex code

Hi All,

 

How to send chatter private messages from Apex code? Any example code ?

 

Thanks in Advance

komathipriyakomathipriya

Hi chamil, 

 

use feeditem to insert message into the chatter objcet. 

 

sample code is below:: 

List<FeedItem> feedItems = new List<FeedItem>(); 

for (Case cas: Trigger.new) {
FeedItem fitem = new FeedItem();
fitem.ParentId = cas.id;
fitem.LinkUrl = '/' + cas.id; //This is the url to take the user to the activity
fitem.Title = 'View'; //This is the title that displays for the LinkUrl
fitem.Body = ((Trigger.isInsert)? ' New Case has been Created ' : ' Case has been updated')
feedItems.add(fitem);
}
//Save the FeedItems all at once.
if (feedItems.size() > 0) {

Database.insert(feedItems,false); //notice the false value. This will allow some to fail if Chatter isn't available on that object

}