You need to sign in to do that
Don't have an account?
d3developer
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...
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
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
It works! Cool!
I'll put up some related test code on my blog soon.