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
Christopher PettittChristopher Pettitt 

Flow to avoid the 500 limit for EntitySubscriptions?

I have a Flow/Process Builder that creates an EntitySubscription whenever a Case is created with certain users being assigned as the SubscriberId and the newly created Case as the ParentId. We just ran into an issue where one of our users hit the 500 SubscriberId limit which nearly crashed our entire sandbox. Hoping to avoid that scenario again, I wanted to setup our flow to check if a user is getting close to the 500 limit before creating the EntitySubscription record (and then probably delete the oldest record). I just can't find a good way in the Flow to do a count of records and was hoping someone could help! Thanks in advance! 
Best Answer chosen by Christopher Pettitt
Raj VakatiRaj Vakati
You can write a trigger on the case and delete old ones 
https://salesforce.stackexchange.com/questions/106509/autofollow-using-entitysubscription
List<EntitySubscription> stdEntitySubList = new List<EntitySubscription>();
    if(Trigger.isInsert){
    for(Student__c std:trigger.new){

        EntitySubscription stdEntity = new EntitySubscription();
        stdEntity.ParentId = std.Id;
        stdEntity.SubscriberId = std.Mentor__c;
        stdEntitySubList.add(stdEntity);
     }

        insert stdEntitySubList;
  }
  if(trigger.isUpdate){

      List<EntitySubscription> stdEntitySubListUpdate = new List<EntitySubscription>();
      List<Student__c> stdList = new List<Student__c>();

      for (Student__c std:Trigger.new) {
        Student__c oldStudent = Trigger.oldMap.get(std.ID);
        if (std.Mentor__c != oldStudent.Mentor__c) {
            stdList.add(std);
            }
         }        

       //delete subscription;
       delete[select id from EntitySubscription where ParentId IN :stdList];


       for(Student__c std : stdList){
            EntitySubscription e = new EntitySubscription();
            e.ParentId = std.Id;
            e.SubscriberId = std.Mentor__c;
            stdEntitySubListUpdate.add(e);
        }

      insert stdEntitySubListUpdate;
  }

 

All Answers

Raj VakatiRaj Vakati
You can write a trigger on the case and delete old ones 
https://salesforce.stackexchange.com/questions/106509/autofollow-using-entitysubscription
List<EntitySubscription> stdEntitySubList = new List<EntitySubscription>();
    if(Trigger.isInsert){
    for(Student__c std:trigger.new){

        EntitySubscription stdEntity = new EntitySubscription();
        stdEntity.ParentId = std.Id;
        stdEntity.SubscriberId = std.Mentor__c;
        stdEntitySubList.add(stdEntity);
     }

        insert stdEntitySubList;
  }
  if(trigger.isUpdate){

      List<EntitySubscription> stdEntitySubListUpdate = new List<EntitySubscription>();
      List<Student__c> stdList = new List<Student__c>();

      for (Student__c std:Trigger.new) {
        Student__c oldStudent = Trigger.oldMap.get(std.ID);
        if (std.Mentor__c != oldStudent.Mentor__c) {
            stdList.add(std);
            }
         }        

       //delete subscription;
       delete[select id from EntitySubscription where ParentId IN :stdList];


       for(Student__c std : stdList){
            EntitySubscription e = new EntitySubscription();
            e.ParentId = std.Id;
            e.SubscriberId = std.Mentor__c;
            stdEntitySubListUpdate.add(e);
        }

      insert stdEntitySubListUpdate;
  }

 
This was selected as the best answer
Christopher PettittChristopher Pettitt
Thanks for the help Raj V! Is there a way to have a flow call an apex trigger or are you just saying to scrap the flow completely and just use a trigger for what we want to do?  
Raj VakatiRaj Vakati
I belive you have already trigger on the case .. cant you invoke this code from ther trigger ??/ 

If not try to move the code to trigger