Friday 14 October 2016

LIMIT_EXCEEDED, Maximum per user subscription limit reached


          Problem : 

Chatter EntitySubscription Limits ?

In my project, we follow certain records via after update triggers, if some conditions match.

I am getting this error on follow code that creates an EntitySubscription via trigger

Description: Error:Apex trigger FollowXYZ caused an unexpected exception, contact your administrator: FollowXYZ: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: LIMIT_EXCEEDED, Maximum per user subscription limit reached.: []:

Solution :
  • Before Inserting the 'EntitySubscription' record, Get the count() subscription-id. 
  • If the subscription-id count() is greater then 500, then delete the older EntitySubscription records first, then proceed with inserting the new EntitySubscription records.

Code Snippet :

1) Count(userID)

SELECT count() FROM EntitySubscription  WHERE SubscriberId = '00530000005nnYnAAI'   AND ParentId IN ( SELECT Id FROM CASE WHERE CreatedDate = LAST_N_DAYS:365 AND Status      = 'Solved')

2) Delete(userID)


SELECT id, FROM EntitySubscription

WHERE SubscriberId = '00530000005nnYnAAI'
   AND ParentId IN (
 SELECT Id FROM CASE 
WHERE CreatedDate = LAST_N_DAYS:365 AND
 Status ='Solved' 

) Order by CreatedDate asc


Ex : Delete [SELECT id FROM EntitySubscription WHERE SubscriberId = '005a0000007pKz9AAE'  AND ParentId IN (SELECT Id FROM CASE WHERE CreatedDate = LAST_N_DAYS:365 AND Status ='Solved' ) Order by CreatedDate asc];