Generic Lightning Component for Follow/UnFollow Record

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


After clicking on the Follow button


After mouse over on Following button


Take a look at the below components


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

[sourcecode language=”java”]</pre>
<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}” />


[sourcecode language=”java”]
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;


[sourcecode language=”java”]({
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.

[sourcecode language=”java”]<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 }”


[sourcecode language=”java”]({
follow : function(component, event, helper) {


[sourcecode language=”java”]({
// 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.’);


[sourcecode language=”java”]
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 Comment

Your email address will not be published. Required fields are marked *

Recent Posts

salesforce for financial services transforming customer engagement operational effectiveness
Salesforce for Financial Services: Transforming Customer Engagement & Operational Effectiveness
einstein copilot transform your salesforce experience
How Einstein Copilot Actions Can Transform Your Salesforce Experience
how can salesforce experience cloud drive customer engagement rates
How Can Salesforce Experience Cloud Drive Customer Engagement Rates?
salesforce world tour essentials mumbai
Salesforce World Tour Essentials Mumbai (19 June, 2024)
benefits of using salesforce for higher education Industry
Benefits Of Using Salesforce For Higher Education Industry?
Scroll to Top