How to follow entities (Accounts, Contact, Opportunities, Custom Objects) in Chatter?

SFDC-Interview-Questions-AnswersBy Adding records to the ‘EntitySubscription‘ object. This object is available only through the API.

EntitySubscription Objects stores which user follows which Account, Contact, or any custom object Record or UserThe fields in this table are
ParentId: The ID of the Account, Contact or any record or User.
SubscriberId: The ID of the user who follows the record denoted in the Parent ID.

To Add followers automatically using Apex Class/Trigger, you could insert records into this object. And to Un-follow just delete the relevant records.

EntitySubscription newEntity = new EntitySubscription();
newEntity.ParentId = 'insert relevant SFDC ID';   //Account, Contact, Custom Object...
newEntity.SubscriberId = 'insert User ID'; //USER ID
insert newEntity;

To Retrive the Follower Information:
For Example:

List myAccoutFollower = [Select Id, ParentId, SubscriberId from EntitySubscription where ParentId = '001Q11100046Vt3'];

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

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