You need to sign in to do that
Don't have an account?

Remove chatter followers?
I've scoured the net and SF boards, but I could not find a single example of how to remove Chatter followers from an object. I'm pretty sure I did it right by just removing the correct EntityRelationship record, but the test method is failing. Either I wrote the removeAllFollowers method wrong, or I wrote the test wrong. Can you take a look and tell me if you see anything obvious? Thanks!
Goal: When a Project__c is closed, remove all Chatter followers.
Problem: When I run my getFollowers() method two different times on the same Project__c object, I get two different records. Driving me crazy.
public static Boolean removeAllFollowers(Set<Id> objectToUnfollowIdSet) {
List<EntitySubscription> subscriptionsToDeleteList =
[SELECT e.Id, e.parentId, e.subscriberId
FROM EntitySubscription e
WHERE e.parentId IN :objectToUnfollowIdSet];
try {
delete subscriptionsToDeleteList;
} catch (System.Exception e) {
throw e;
return false;
}
return true;
}
public static Map<Id, Set<Id>> getFollowers(List<Id> objectIdList) {
Map<Id, Set<Id>> objectToFollowerMap = new Map<Id, Set<Id>>();
Set<Id> followerIdSet = new Set<Id>();
List<EntitySubscription> subscriberList = [SELECT e.ParentId, e.SubscriberId
FROM EntitySubscription e
WHERE e.ParentId IN :objectIdList];
for (Id objectId : objectIdList) {
followerIdSet.clear();
for (EntitySubscription subscriber : subscriberList) {
if (subscriber.parentId == objectId) {
followerIdSet.add(subscriber.subscriberId);
}
}
System.debug('objectId: ' + objectId + ' followerIdSet: ' + followerIdSet);
objectToFollowerMap.put(objectId, followerIdSet);
}
return objectToFollowerMap;
}
static testMethod void testProjectCloses_HasChatterFollowers() {
// Create test data
User user1 = TestHelper.createUser('first', 'last', 'email@email.com', TestHelper.getSysAdminProfile().Id);
Project__c project1 = TestHelper.createProject('project1', user1.Id);
User user2 = TestHelper.createUser('bob', 'follower', 'email2@email.com', TestHelper.getSysAdminProfile().Id);
EntitySubscription subscription = TestHelper.createChatterFollower(project1.Id, user2.Id);
System.debug('SUBSCRIPTION: ' + subscription);
// Ensure data is as expected
Map<Id, Set<Id>> followersMap = ChatterHelper.getFollowers(new List<Id>{project1.Id});
// This debug gives an ID of X
System.debug('FOLLOWERSMAP: ' + followersMap);
System.assertEquals(1, followersMap.size());
// Invoke functionality
System.Test.startTest();
project1.Status__c = 'Closed';
update project1;
System.Test.stopTest();
// Check results
project1 = [SELECT p.Id, p.Status__c FROM Project__c p WHERE p.Id = :project1.Id];
followersMap = ChatterHelper.getFollowers(new List<Id>{project1.Id});
// This debug gives an ID of Y
System.debug('FOLLOWERSMAP: ' + followersMap);
// Failing here on this assert
System.assertEquals(0, followersMap.size());
}
Nice find! Thanks for checking that out! I wish the boards had better css for pasting code (unless I didn't see it), so it must have been ugly for you to look at. You're find did solve one of my problems, but I still had another.
As it turns out, we had a trigger (that I was unaware of) that was adding the creator of the project as a chatter follower automatically. I couldn't figure out why there was still a chatter follower even after I deleted it. In my debug statements, I could see the user ID of this follower, which was different than the one I added earlier, and I was thinking "Who the eff is still following this project?! I deleted you! Who is this **bleep**!?" I did an extra query to see who this **bleep** was, and when I went to the debug logs to see who it was.....it was me. :-( The creator of the project. Wasted debugging time aside, pretty comical.
Summary: My code is correct (with rwoollen's fix on the getFollowers method). But, another trigger was doing unforeseen things to mess with my test.
All Answers
It looks like your getFollowers() puts an entry into the map even if there are no followers.
-- Rob
Nice find! Thanks for checking that out! I wish the boards had better css for pasting code (unless I didn't see it), so it must have been ugly for you to look at. You're find did solve one of my problems, but I still had another.
As it turns out, we had a trigger (that I was unaware of) that was adding the creator of the project as a chatter follower automatically. I couldn't figure out why there was still a chatter follower even after I deleted it. In my debug statements, I could see the user ID of this follower, which was different than the one I added earlier, and I was thinking "Who the eff is still following this project?! I deleted you! Who is this **bleep**!?" I did an extra query to see who this **bleep** was, and when I went to the debug logs to see who it was.....it was me. :-( The creator of the project. Wasted debugging time aside, pretty comical.
Summary: My code is correct (with rwoollen's fix on the getFollowers method). But, another trigger was doing unforeseen things to mess with my test.