ABSYZ ABSYZ

  • Home

    Welcome to ABSYZ

  • About us

    Who We Are

  • Our Expertise

    What We Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Generic Lightning Component for Follow/UnFollow Record

Home / Article & Blogs / Salesforce / Lightning / Generic Lightning Component for Follow/UnFollow Record

Generic Lightning Component for Follow/UnFollow Record

By Chandrasekhar Mokkala inLightning, Salesforce

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

DisplayCaseRecords.cmp

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”>
<thead>
<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>
</tr>
</thead>
<aura:iteration items=”{!v.caseWrapList}” var=”caseWrap” >
<tbody>
<tr>
<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>
<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}” />
</lightning:buttonGroup></td>
</tr>
</tbody>
</aura:iteration></table>
</aura:component>
<pre>[/sourcecode]

DisplayCaseRecordsController.js

[sourcecode language=”java”]
({
doInit : function(component, event, helper) {
helper.getCaseRecords(component);
},
navigateTOCaseRecord : function(component, event, helper) {
var recordId=event.getSource().get(“v.value”);
window.open(‘/’ + recordId);&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; &amp;amp;nbsp;
}
})
[/sourcecode]

DisplayCaseRecordsHelper.js

[sourcecode language=”java”]({
getCaseRecords : function(component) {
var action=component.get(“c.getCaseRecords”);
action.setCallback(this, function(response) {
var state = response.getState();
if (state === “SUCCESS”){
component.set(“v.caseWrapList”,response.getReturnValue());
}
else if(state === “ERROR”){
alert(‘Problem with connection. Please try again.’);
}
});
$A.enqueueAction(action);
}
})
[/sourcecode]

FollowOrUnFollowRecord.cmp

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”
labelWhenHover=”Unfollow”
iconNameWhenOff=”utility:add”
iconNameWhenOn=”utility:check”
iconNameWhenHover=”utility:close”
state=”{!v.isFollowed}”
onclick=”{!c.follow }”
/>
</aura:component>[/sourcecode]

 

FollowOrUnFollowRecordController.js

[sourcecode language=”java”]({
follow : function(component, event, helper) {
helper.followRecord(component);
}
})
[/sourcecode]

FollowOrUnFollowRecordHelper.js

[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”);
action.setParams({
“recordId” : component.get(“v.recordId”)
“isFollowed” : component.get(“v.isFollowed”)
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === “SUCCESS”)
{
component.set(“v.isFollowed”,response.getReturnValue())
}
else if(state === “ERROR”)
{
alert(‘Problem with connection. Please try again.’);
}
});
$A.enqueueAction(action);
}
})
&amp;lt;span&amp;gt;[/sourcecode]

 

FollowUnFollowRecordController.apexc

[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){
caseIds.add(entitysubscription.ParentId);
}        }        return caseIds;
} // This method is used to return the case Records.
@AuraEnabled
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(ca.id) ));
}        return caseWrapList;    }
//This method is used to Follow/UnFollow record.
@AuraEnabled
public static Boolean followRecord(String recordId,Boolean isFollowed) {
try{
if(!isFollowed){
EntitySubscription entitysubscription = new EntitySubscription(parentId=recordId, SubscriberId=UserInfo.getUserId());
insert entitysubscription;
}
if(isFollowed){
EntitySubscription entitysubscription = [SELECT id FROM EntitySubscription WHERE parentId =: recordId AND subscriberId =: UserInfo.getUserId()];
delete entitysubscription;
}
return !isFollowed;
}catch(Exception except){
system.debug(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;
}
}
}[/sourcecode]

 

Please feel free to comment.

Case FeedEmailGeneric componentLightning componentRecord Tracking
43
Like this post
1 Post
Chandrasekhar Mokkala

Search Posts

Archives

Categories

Recent posts

Significance of UI/UX Design

Significance of UI/UX Design

Cyber-security in an uncertain world

Cyber-security in an uncertain world

The world of AR and VR

The world of AR and VR

The in-and-out of ML

The in-and-out of ML

Future of Mobility

Future of Mobility

  • Previous PostIntegrating Salesforce and Microsoft Outlook by using Lightning for Outlook
  • Next PostReusable Lightning Notification On Record Update (Backend/API/UI) using Lightning Data service

Related Posts

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

Create/Update Salesforce Picklist definitions using metadata API
Integration Metadata API Salesforce

Create/Update Salesforce Picklist definitions using metadata API

1 Comment

  1. Dhanith
    Reply
    4 July 2019

    Can you please send the code for normal follow button Where when the follow button is clicked it by users they should follow it and when they click unfollow button they should unfollow it

    Reply

Leave a Reply (Cancel reply)

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

*
*

ABSYZ Logo
  • Home
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy