Lightning · Salesforce

Generic Lightning Component for Follow/UnFollow Record

It is often useful for a user to keep track updates on the case feed this will helps to resolve the cases more efficiently. Salesforce provides “Follow” feature, Using this feature we can track updates to a record, including field changes, posts, tasks, and comments on records.

If the user follows a record, he can see in his feed depend on which of the fields your administrator configured for feed tracking and he will get updates according to his email settings.

Follow the below steps to enable Chatter Email Settings or Email Notifications :

  1. Go to Name and click on SetUp.
  2. Click on My Settings.
  3. Search Chatter in the Quick Find box.
  4. Click on Email Notifications and Check/Uncheck checkboxes.

Limitations with Standard Salesforce Follow Feature:

  1. we can’t see Updates to encrypted custom fields in feeds.
  2. we can follow a maximum combined total of 500 people, topics, and records.

We built a custom “Follow” Functionality which will work like Standard Salesforce Follow functionality but this can be used in any Lightning Page and it is generic component.

Components List:

  1. DisplayCaseRecords
  2. FollowOrUnFollowRecord(Generic Component)

Apex Controller:

  1. FollowUnFollowRecordController

Generic component picture


After clicking on the Follow button

After Clicked on Follow button


After mouse over on Following button

Mouseover on Following

Take a look at the below components


This component is used to display case records and Follow / UnFollow records.

<aura:component controller="FollowUnFollowRecordController" implements="force:appHostable,flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

<!-- Internal Attributes -->
<aura:attribute name="caseWrapList" type="Object[]" />

<!-- Handlers -->
<aura:handler name="init" value="this" action="{!c.doInit}"/>
<table class="slds-table slds-table_bordered slds-table_cell-buffer">
<tr class="slds-text-title_caps">
<th scope="col">
<div class="slds-truncate" title="Subject"> Subject</div></th>
<th scope="col">
<div class="slds-truncate" title="Status"> Status</div></th>
<th scope="col">
<div class="slds-truncate" title="Priority" > Priority</div></th>
<aura:iteration items="{!v.caseWrapList}" var="caseWrap" >
<td data-label="Subject" >
<div class="slds-truncate" title="Subject" > {!caseWrap.caseRec.Subject}</div></td>
<td data-label="Status">
<div class="slds-truncate" title="Status"> {!caseWrap.caseRec.Status}</div></td>
<td data-label="Priority">
<div class="slds-truncate" title="Priority"> {!caseWrap.caseRec.Priority}</div></td>
		<lightning:buttonGroup >
<c:FollowOrUnFollowRecord recordId="{!caseWrap.caseRec.Id}" isFollowed="{!caseWrap.isFollowed}"/>
		<lightning:button value="{!caseWrap.caseRec.Id}" variant="neutral" label="More Info" iconPosition="left" onclick="{!c.navigateTOCaseRecord}" />


doInit : function(component, event, helper) {
navigateTOCaseRecord : function(component, event, helper) {
 var recordId=event.getSource().get("v.value");'/' + recordId);&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; &amp;amp;nbsp;


getCaseRecords : function(component) {
var action=component.get("c.getCaseRecords");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS"){
else if(state === "ERROR"){
alert('Problem with connection. Please try again.');


This is the Generic component, In which I am using lightning:buttonStateful component represents a button which will toggle between states. By default “Follow” will display, “Following” will display when we click on the button. We can use icons along with text label but It supports only Utility category icons.

<aura:component controller="FollowUnFollowRecordController" access="global" >
<!-- External Attributes -->
<aura:attribute name="recordId" type="string" description="the record Id of the any object" />
<aura:attribute name="isFollowed" type="Boolean" default="false" description="check if the record is already followed by current user"/>
<!-- Component Core -->
		<lightning:buttonStateful labelWhenOff="Follow" labelWhenOn="Following"
onclick="{!c.follow }"



 follow : function(component, event, helper) {


 // followRecord Method used to Follow/Unfollow a record
 followRecord : function(component) {
 var recordId = component.get("v.recordId");
 var isFollowed = component.get("v.isFollowed");
 var action=component.get("c.followRecord");
 "recordId" : component.get("v.recordId")
 "isFollowed" : component.get("v.isFollowed")
 action.setCallback(this, function(response) {
 var state = response.getState();
 if (state === "SUCCESS")
 else if(state === "ERROR")
 alert('Problem with connection. Please try again.');



public class FollowUnFollowRecordController {
// To store case Ids.
public static set<Id> caseIds {get;set;}
// This method is used to check the record is already followed by current user or not.
public static Set<Id> getMyFollowedAccntId() {
if (caseIds == null){
List<EntitySubscription> lstEntitySubscription = new List<EntitySubscription>();
caseIds = new set<ID>();
lstEntitySubscription = [SELECT ParentId FROM EntitySubscription WHERE SubscriberId =: UserInfo.getUserId() AND  Parent.Type ='Case' ORDER By CreatedDate DESC LIMIT 1000];
for (EntitySubscription entitysubscription :lstEntitySubscription){
}        }        return caseIds;
} // This method is used to return the case Records.
public static List<caseWrapper> getCaseRecords() {
List<caseWrapper> caseWrapList = new List<caseWrapper>();
for(case ca: [Select id,subject,status,priority,caseNumber from case limit 10 ]){
caseWrapList.add(new caseWrapper(ca, getMyFollowedAccntId().contains( ));
}        return caseWrapList;    }
//This method is used to Follow/UnFollow record.
public static Boolean followRecord(String recordId,Boolean isFollowed) {
EntitySubscription entitysubscription = new EntitySubscription(parentId=recordId, SubscriberId=UserInfo.getUserId());
insert entitysubscription;
EntitySubscription entitysubscription = [SELECT id FROM EntitySubscription WHERE parentId =: recordId AND subscriberId =: UserInfo.getUserId()];
delete entitysubscription;
return !isFollowed;
}catch(Exception except){
return null;
return true;
/*    * @desc    Case wrapper class    */
public class caseWrapper{
@AuraEnabled        public Case caseRec {get;set;}
@AuraEnabled        public Boolean isFollowed {get;set;}
// constructor
public caseWrapper(){}
// constructor with paramters.
public caseWrapper(Case caseRec, Boolean isFollowed){
this.caseRec = caseRec;
this.isFollowed = isFollowed;


Please feel free to comment.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s