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
Shaker Kuncham 1Shaker Kuncham 1 

how to remove followers when case is closed

I use the "Follow" feature on Cases to keep track of activity on them via
the SalesForce Chatter desktop application. Every couple of months I get a
notification when trying to follow a case that I've reached my maximum
amount of "Follows," which I believe is 500. This is because I'm continuing
to follow cases that have been Closed.

Is it possible to configure so that once a case is Closed it automatically
removes any followers?


Regards
Shaker
Best Answer chosen by Shaker Kuncham 1
Bhanu MaheshBhanu Mahesh
Hi Shaker,

You can do this using trigger on Case.

trigger RemoveFollowers on Case (after update) {
    Set<Id> caseIds = new Set<Id>();
    for(Case cas : trigger.new){
        if(cas.IsClosed){
            caseIds.add(cas.Id);
        }
    }
    if(!caseIds.isEmpty()){
        List<EntitySubscription> lstFollwersToBeDeleted = new List<EntitySubscription>();
        lstFollwersToBeDeleted = [SELECT Id FROM EntitySubscription WHERE ParentId IN : caseIds];
        if(!lstFollwersToBeDeleted.isEmpty()){
            delete lstFollwersToBeDeleted ;
        }
    }


Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi Shaker,

You can do this using trigger on Case.

trigger RemoveFollowers on Case (after update) {
    Set<Id> caseIds = new Set<Id>();
    for(Case cas : trigger.new){
        if(cas.IsClosed){
            caseIds.add(cas.Id);
        }
    }
    if(!caseIds.isEmpty()){
        List<EntitySubscription> lstFollwersToBeDeleted = new List<EntitySubscription>();
        lstFollwersToBeDeleted = [SELECT Id FROM EntitySubscription WHERE ParentId IN : caseIds];
        if(!lstFollwersToBeDeleted.isEmpty()){
            delete lstFollwersToBeDeleted ;
        }
    }


Regards,
Bhanu Mahesh
This was selected as the best answer
Shaker Kuncham 1Shaker Kuncham 1
Hi Bhanu

Thanks for your quick response on my question.
Already i have trigger on case and i have used the same trigger and modifed the code, does it works like this please suggest.

trigger caseOwnerUpdate on Case (after update) {
    List<Case> updateCS = new List<Case>();
    Map<Id,Case> cases = new Map<Id,Case>();
    

    for (Case cs : Trigger.new)
    {
        if(Trigger.isUpdate && Trigger.isAfter){
            // To unsubscibe the feed from case if case status is closed.
            updateFeedSubscription(cs);

        }
        if(Trigger.isUpdate) {  
            System.debug('>>>>> Owner ID: '+cs.ownerId+' Temp Owner ID: '+cs.TempOwnerId__c);
            if(cs.TempOwnerId__c <> null && cs.TempOwnerId__c <> '') {
                if(cs.OwnerId <> cs.TempOwnerId__c) {
                    cases.put(cs.id,cs);
                }
            } 
            
        }   
    }
    if (cases.isEmpty()) return;
    
    for (Case cs : [SELECT OwnerId,TempOwnerId__c FROM Case WHERE id in :cases.keySet()]) {
        cs.OwnerId = cases.get(cs.Id).TempOwnerId__c;
        cs.TempOwnerId__c = 'SKIP'; //flag to stop infinite loop upon update
        updateCS.add(cs);
    }
    System.debug('>>>>>Update Cases: '+updateCS);
    
    //
    //Update last assignment for Assignment Group in batch
    //
    if (updateCS.size()>0) {
        try {
            update updateCS;
        } catch (Exception e){

        }
    }
    // This code will execute to remove followers on closed case.
   public void updateFeedSubscription(Case c){
       List <EntitySubscription> updateEntityList = new List <EntitySubscription> ();
       System.debug('Hello' +c.Status);
       if(c.Status == 'Closed'){
           //List of EntitySubscriptions for the case
           for(EntitySubscription e : [select id,subscriberid,parentid from EntitySubscription where parentid = :c.Id ] ){
               System.debug('EntitySubscription ' + e);
               updateEntityList.add(e);
           }
           //deleting the EntitySubscription to unfollow for the case.
           delete updateEntityList;

       }
    }
}

 
Bhanu MaheshBhanu Mahesh
Hi Shaker,

It will work, but the problem is you are calling this method for every record and in the method you have SOQL query which is in for Loop which is not a best practice.

So try to pass the list or set of Case Ids whose status is closed to the method.

If you are doing dml operations check whether any elements are there or not in the list.

Regards,
Bhanu Mahesh
Shaker Kuncham 1Shaker Kuncham 1
Hi Bhanu

thanks for your replay, i will implement the same. Thanks for your suggestion.

Regards
Shaker
Shaker Kuncham 1Shaker Kuncham 1
Hi Bhanu

Requirement : On a business day when case comment reaches to 5 , it should trigger out email to my manager with the  5th  comment 
When our agent update the same case on next day it should increment to 6,7,8, again if it reaches to 10 , with the workflow it will notify the manager.

I have created field called daily comments and worte the trigger, however sometimes it is working and somestimes not,
Please give your suggestions on the below trigger.

/ This trigger is used to update the daily count  comments of case
//  when a case comment is created.
trigger CaseCommentTrigger on CaseComment (After insert) {

    update_CaseComment_Count();
     

 public void update_CaseComment_Count(){
    List <Case> caseList = new List<Case>();
    List <Case> update_caseList = new List<Case>();    
     try{
    for(CaseComment cc : Trigger.New){
      if(Trigger.isInsert && Trigger.isAfter) {
         caseList = [select id,Daily_Comment_Count__c from case where Status !='Closed' AND id=:cc.ParentId ];
         List<CaseComment> cscomment = [select id from CaseComment where ParentId = :cc.ParentId AND CreatedDate > YESTERDAY ];
          
          System.debug('cscomment List' + cscomment + ' ::size :: ' + cscomment.size());
          for(case c : caseList ){
              // In a day the count will start with zero and if the count is greater than 5 resetting back to zero
            
             if(c.Daily_Comment_Count__c == null)
                 c.Daily_Comment_Count__c = 0;  
          
             c.Daily_Comment_Count__c = c.Daily_Comment_Count__c + 1 ;
             
             c.Last_Updated_Comment__c = cc.CommentBody;
             
              if(system.Math.mod(cscomment.size(),5) == 0){
               c.Daily_Comment_Count__c = 5;   
             }
             update_caseList.add(c);
             
                 
             System.debug('case comment count is: ' +c.Daily_Comment_Count__c);
         }
          update update_caseList;
      }
    }
     } catch(Exception e){
         system.debug('Stack Trace' + e.getStackTraceString());
         system.debug('Cause : '+ e.getCause() + 'Message : ' + e.getMessage());
     }
  }
}