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 

3x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published.