How to follow entities (Accounts, Contact, Opportunities, Custom Objects) in Chatter?
By 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:
List myAccoutFollower = [Select Id, ParentId, SubscriberId from EntitySubscription where ParentId = '001Q11100046Vt3'];