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
kittu9kittu9 

Need help to identigy each particular user Entity subscription count???

Req: I have no.of users in my instance and no.of entity subscriptions in my instance. Have to identify each individual users no.of entity subscription based on that want to delete some entity subscriptions of that user.  Have to write code by avoiding governor limits can any one help me on this

 

Step 1: List of users (No.of user in instance eg: 30 to 40 users)

 

Step 2: List on Entity subscriptions (Have to identify which user created how many Entity subscriptions)

 

Step 3: If particular user E.S count > 490 want to delete first 25 E.S of that user.

 

I written code but its crossing governor limits. Can any one help me on this ASAP. Thanks in advance

Sean TanSean Tan

I've taken logic from your code and ported it to something that is safer in bulk. Based off what I've seen in the metadata to query the entitysubscriptions off the User the child table is called FeedSubscriptions:

 

public void deleteSubscriptions()
{    
    Map<Id, Integer> userMapCount = new Map<Id, Integer>{};
    Set<Id> usersToCleanupSubscriptions = new Set<Id>{};
    
    for (User u : [ select Id from User ])
    {
        userMapCount.put(u.Id, 0);
    }
    
    for (EntitySubscription es : [ SELECT Id, SubscriberId FROM EntitySubscription  ])
    {
        Integer val = userMapCount.get(es.SubscriberId);
        
        if (val != null && !usersToCleanupSubscriptions.contains(es.SubscriberId))
        {
            val++;
            userMapCount.put(es.SubscriberId, val);
            
            if (val > 490)
            {
                usersToCleanupSubscriptions.add(es.SubscriberId);
            }
        }
    }
    
    EntitySubscription[] esListToDelete = new EntitySubscription[]{};
    
    for (User u : [ select Id, (SELECT Id FROM FeedSubscriptions WHERE ParentId NOT IN :userMapCount.keySet() ORDER BY CreatedDate LIMIT 25) FROM User WHERE Id IN :usersToCleanupSubscriptions])
    {
        esListToDelete.addAll(u.FeedSubscriptions);
    }        
    
    delete esListToDelete;        
}

 

RocketRocket

Hi Sean ,

 

Can u please tell us how did you saw feedsubscription object in Metadata, When I look at the Salesforfce ERD's I do not come across this object ?

Thanks

 

Sean TanSean Tan

I used Force.com explorer to look up the metadata schema:

 

http://wiki.developerforce.com/page/ForceExplorer

RocketRocket

Hi sean,

 

I know I am not as good as you are .However,I do not understand how u have come to the conclusion that feed subscription has some role to play in this when the person who has posted this does not even  talk about it in his code.

 

Anyway,I must be missing something here?

 

 

 

Sean TanSean Tan

His code was focused on the EntitySubscription, based off what I read up in the API doc it related back to chatter follows. From there I simply tried it out by using a test org and added a chatter follow, then used Force.com Explorer to find a way to query that record straight from the User object.