Category Archives: Chatter

Chatter Series – Chatter Connect (Chatter in Apex)

In my previous blog, on a high level, I described what Chatter is and all the components involved with Chatter. This blog will focus more on building custom Chatter components.

The Why?

Chatter Connect comprises Chatter REST API and Chatter in Apex.

Use Chatter REST API to integrate mobile apps, intranet sites, and 3rd party Web applications with Salesforce. Chatter REST API provides resources for feeds, comments, likes, users, groups, private messages, recommendations, topics, and more. Chatter REST API is on by default in all organizations and editions that have Chatter.

Use Chatter in Apex to create custom experiences in Salesforce. Many Chatter REST API resource actions are exposed as static methods in Apex classes in the ConnectApi namespace. These classes are called Chatter in Apex. Chatter in Apex is available in all organizations and editions that have Apex enabled.

My series will go into detail on Chatter in Apex, in other words, the ConnectApi namespace. If you are too busy to go over the details and want just code, please navigate to my Github Repo.

Chatter Feed

chatter_2

The ConnectApi namespace is logically categorized into input and output classes. As the names suggest, anything that helps in retrieving or displaying the Chatter feed, likes, replies etc. uses  output classes. Any action (e.g., posting a feed, liking a post, replies, etc.) that is performed by the User on Chatter and is written to the Database uses input classes.

NOTE: with Salesforce API Version 32.0 and above, there are many changes in the structure of the ConnectApi classes. Please make sure that you use v32.0 or above when using my code from the blog. 

Output Classes

FeedElementPage

 

The image above gives an overview of the structure of Chatter Output Classes that I will be using in the code, (FeedElementPage). A Chatter post is a list of FeedElements that are part of FeedElementPage.

Note: For a comprehensive list of Chatter classes, please click here.

The main method that is used to retrieve the Chatter feed is getFeedElementsFromFeed. Below is a code sample to get the feed. There are many overloaded versions of the same method with various arguments. In the code below, the first argument is communityId or NetworkId, the second is the type of Feed (in our case Record enum), and the third is the groupId.


ConnectApi.FeedElementPage feedElementPage = ConnectApi.ChatterFeeds.getFeedElementsFromFeed(communityId,ConnectApi.FeedType.Record,groupid);
return feedElementPage;

Now that we have the feeds, all we need to do is display them on a page. I am using Handlebars.js, Handlebars.js is a templating library that allows us to create semantic templates. This way it’s much cleaner and our JavaScript and HTML are truly decoupled.

The Handlebar template below displays the list of feeds on a Visualforce page with Bootstrap styling.


<script id="feed_template" type="text/x-handlers-template">
{{#if elements.length}}


<ul class="media-list">
{{#each elements}}


	<li class="media">


<div class="media-left">
<a href="#">
<img class="media-object" src="{{actor.photo.smallPhotoUrl}}" alt="{{actor.name}}" style="height:50px;">
</a>
</div>



<div class="media-body">


<h4 class="media-heading" style="font-size:18px; color:#015ba7">{{actor.name}}</h4>


{{#each body.messageSegments}}
{{#isTextSegment type}}
{{text}}
{{/isTextSegment}}
{{#isMentionSegment type}}
<a href="#">{{record.displayName}}</a>
{{/isMentionSegment}}
{{/each}}


<div class="media-footer">
<a href="#" class="postfeed-comment" style="font-size:12px;" data-feedid="{{id}}">Comment</a>&nbsp;&nbsp;
{{#if capabilities.chatterLikes.isLikedByCurrentUser}}
<a href="#" class="postfeed-like unlikeBtn" style="font-size:12px;" data-feedid="{{id}}" data-likeid="{{capabilities.chatterLikes.myLike.id}}">Unlike</a>&nbsp;
{{else}}
<a href="#" class="postfeed-like likeBtn" style="font-size:12px;" data-feedid="{{id}}">Like</a>&nbsp;
{{/if}}
<em style="font-size:12px;"> {{dateFormat createdDate}} </em>
</div>


{{#if capabilities.chatterLikes.likesMessage}}


<div class="like-footer">
<em style="font-size:12px;">{{capabilities.chatterLikes.likesMessage.text}}</em>
</div>


{{/if}}
{{#each capabilities.comments.page.items}}


<div class="media">


<div class="media-left">
<a href="#">
<img class="media-object" style="height:40px;" src="{{user.photo.smallPhotoUrl}}" alt="{{user.name}}">
</a>
</div>



<div class="media-body">


<h4 class="media-heading" style="font-size:16px;">{{user.name}}</h4>


{{#each body.messageSegments}}
{{#isTextSegment type}}
{{text}}
{{/isTextSegment}}
{{#isMentionSegment type}}
<span style="font-color:blue;">{{record.displayName}}</span>
{{/isMentionSegment}}
{{/each}}


<div class="media-footer">
<a href="#" class="postfeed-like" style="font-size:12px;" data-feedid="{{id}}">Like</a>&nbsp;
<em style="font-size:12px;"> {{dateFormat createdDate}} </em>
</div>


</div>


</div>


{{/each}}
</div>



<div class="comment body" data-feedid="{{id}}">
<textarea class="form-control sharearea" rows="1" placeholder="Comment" id="reply_{{id}}"></textarea>
<button class="btn btn-alt btn-comment pull-right btn-sm replybtn" hidefocus="true" id="replybtn_{{id}}" data-feedid="{{id}}">
<span>Reply</span>
</button>
</div>


</li>


{{/each}}
</ul>


{{/if}}
</script>

I am using JavaScript remoting on document ready, which calls the controller’s getFeedData method (as shown above) that returns a FeedElementPage object.


function getFeedsForGroup() {
// Call the @RemoteAction JavaScript function
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.ChatterFeedController.getFeedData}',
'{!subjectid}', function(result, event) {
console.log('result');
console.log(result);
feedItems = result.elements;
console.log(feedItems);
buildTemplate();
});
}

function buildTemplate() {
var record_data = {"elements":feedItems};
var result_html = feedTemplate(record_data);
$('#comments').html(result_html);
}

Thats it. Now we have a page that displays the list of Chatter Posts. Isn’t it fun? In the next post, we will go over the input classes that record a feed, a like, a reply, mentions. Until then, happy coding 🙂

 

Chatter Series – Anatomy of Chatter

The first question anyone starting with Salesforce would ask is “What is Chatter ?” There are many ways in which Chatter can be described, but the classic definition that Salesforce repeatedly says is:

Chatter is a private Enterprise Social Network connecting employees inside of your company with each other, business data and content for peak individual and team performance.

From a Developer’s perspective there is more than a definition to Chatter. It has its own feature-set, functionality, API, capabilities. etc. In this post, I will cover the basics of Chatter. In the following posts I will take a deep dive by building custom Chatter Components using cutting edge JavaScript technologies. So, let’s get started.

Anatomy of Chatter

Before getting more into details, let’s get started by knowing all the moving parts of Chatter. When you navigate to the Chatter tab in Salesforce, you can see a page with lots of information as shown in the figure below.

chatter_1

In the following sub-sections, I will give an overview and discuss the importance of various Chatter components.

Chatter Feed

Feed, the most important component in Chatter, is basically a stream of all that’s relevant to you. The feed comprises posts made by the people, object records you follow, and groups you are member of.

Chatter Groups

Group literally means a group that helps you organize the discussions and content. Groups are generally created based on a theme or a topic or a discussion, like Boston User Group, Rest API Group etc. Each group can be either private or public. Based on the group’s visibility, you can search and join a group.

Chatter Post

Post can be anything like a file, a picture or URL – anything that you share within a group or a feed. You can post to a person’s profile or an individual group. You can also “mention” people in your post by using the special keyword ‘@’. (More about mentions in future blogs.)

People

Each person in your organization is automatically part of Chatter and has a public profile. You can view the person’s profile and decide to follow the person’s feed. If you follow the person, all of his/her feeds show up in your Chatter feed.

Bookmarks

If you find a post that you want to refer to at a later point or something that you liked, you can like it or bookmark it. There is a separate section that holds the likes and bookmarks.

Files

All of the files uploaded as part of a post are stored internally in Salesforce where people can collaborate on them. The files show up in a separate section in Chatter.

Topics

Topics are like tags. You can assign topics to a post and also follow existing topics.

Objects

Any object that is enabled for Chatter can be followed. Updates on the record then can be seen in your Chatter feed.

What’s Next?

Hoping that now you have brief idea of what Chatter is, in my next post I will go into using Chatter in Apex via the ConnectAPI namespace. Until then.. Hang on..