function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
d3developerd3developer 

How to make a user follow something?

I've got some Chatter enabled objects. How can I automatically make a user follow them with APEX/Visualforce code?

 

There doesn't seem to be a recipe for this yet...

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Jon Mountjoy_Jon Mountjoy_

Create a trigger on the object that you want to follow, something like:

 

 

trigger addFriendsToUser on User (after insert) { ChatterUtils.addFriendsToUsers (Trigger.newMap.keySet()); }

 

 Now in the code, make the User (in this case) follow something (in this case, a list of other users).

 

 

public class ChatterUtils { public static final List<ID> IDsToFollow = new List<ID> { '005A0000000hFv5' }; // This method is called by the addFriendsToUser trigger on the User object, essentially allowing us to automatically // add a number of friends to a new User. The method is run asynchronously. @future public static void addFriendsToUsers(Set<ID> userIDs) { List<User> users = [select ID from User where ID IN :userIDs]; List<EntitySubscription> allFollowers = new List<EntitySubscription>(); EntitySubscription e = null; for (User u: users) { for (ID i: IDsToFollow) { e = new EntitySubscription(); e.parentId = i; e.subscriberid = u.id; allFollowers.add(e); } } if (!allFollowers.isEmpty()) { insert allFollowers; } }

 

 The key here is EntitySubscription. Simply insert one of these, with the subscriberID set to the item that's doing the following, and parentID to the item being followed.

 

See Recipe 3 and 4 here too.  

 

Jon 

 

 

 

All Answers

Jon Mountjoy_Jon Mountjoy_

Create a trigger on the object that you want to follow, something like:

 

 

trigger addFriendsToUser on User (after insert) { ChatterUtils.addFriendsToUsers (Trigger.newMap.keySet()); }

 

 Now in the code, make the User (in this case) follow something (in this case, a list of other users).

 

 

public class ChatterUtils { public static final List<ID> IDsToFollow = new List<ID> { '005A0000000hFv5' }; // This method is called by the addFriendsToUser trigger on the User object, essentially allowing us to automatically // add a number of friends to a new User. The method is run asynchronously. @future public static void addFriendsToUsers(Set<ID> userIDs) { List<User> users = [select ID from User where ID IN :userIDs]; List<EntitySubscription> allFollowers = new List<EntitySubscription>(); EntitySubscription e = null; for (User u: users) { for (ID i: IDsToFollow) { e = new EntitySubscription(); e.parentId = i; e.subscriberid = u.id; allFollowers.add(e); } } if (!allFollowers.isEmpty()) { insert allFollowers; } }

 

 The key here is EntitySubscription. Simply insert one of these, with the subscriberID set to the item that's doing the following, and parentID to the item being followed.

 

See Recipe 3 and 4 here too.  

 

Jon 

 

 

 

This was selected as the best answer
d3developerd3developer
Can't wait to try on Monday!
d3developerd3developer

It works!  Cool!

 

I'll put up some related test code on my blog soon.