4

My use case is to find total followers on a particular record

So when somebody follows/unfollows that record i would like to update the number of followers of that particular record .stored as a custom field on the object

Possible solution i could think of is have a trigger on entity subscription object ?if so where do we write it?so an after insert/before delete trigger should help in updating?

Or can i avoid code at all?

  • 1
    You cannot write trigger on EntitySubscription – Shebin Mathew Dec 11 '13 at 19:30
0

The object involved in this process is an EntitySubscription. It is the junction between the follower and the object - whether that's another user or a record.

Unfortunately, this object does not support triggers.

If it isn't critical to store this information and it would suffice to just display it on screen, you could implement an inline VF page which uses Recipe #3 to retrieve the count for the current record.

This object is not available as a workflow target so you will need to implement it with a trigger on the EntitySubscription object. The trigger should handle after insert, after update, before delete and after undelete

Here is a chatter recipe link (Recipe #3) which provides a basic query to retrieve the number of followers for a single record. To be used in a trigger, this will need to be altered to handle the worst case scenario of 200 different parentid values at once and update all of those parents with the aggregate result of the number of followers.

If it was an object that supported a trigger, the code would look similar to this:

public class EntitySubscriptionTriggerHelper {
    public static void CalculateNumberOfSubscribers(Map<Id, EntitySubscription> triggerMap) {

        List<Id> entityParentIds = new List<Id>();
        List<Account> accountsToUpdate = new List<Account>();

        for (EntitySubscription subscription : triggerMap.values()) {
            entityParentIds.add(subscription.ParentId);
        }

        // use an aggregate query to calculate the total number of followers
        List<AggregateResult> aggregateResults = [SELECT parentId
                                                        , COUNT(id) subscriberCount 
                                                    FROM EntitySubscription 
                                                    WHERE ParentId IN :entityParentIds
                                                    GROUP BY ParentId];

        for (AggregateResult ar : aggregateResults) {
            accountsToUpdate.add(new Account(Id = String.valueOf(ar.get('parentId'))
                                            , numberOfFollowers__c = Integer.valueOf(ar.get('subscriberCount'))));
        }                            

        if (!accountsToUpdate.isEmpty()) {
            update accountsToUpdate;
        }
    }
}

| improve this answer | |
  • can we write a trigger on entity subscription object?as per my understanding you cannot?..my usecase when user follows a record,update a custom field(which acts like a counter of followers) – sfdc99999 Dec 11 '13 at 18:24
  • 1
    Nope. You sure can't - that's not a standard object which supports triggers. Sorry about that, my answer is totally invalid. – Mark Pond Dec 11 '13 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.