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
Hermann Oure 6Hermann Oure 6 

Iterate through Map to increment count

Hi,
My apex code doesn't not increment the count. Can't find out Why.
Could someone help?
Thanks
Apex class:
public class FeedItemTriggerHandler {
    public static void countFeedItem(List<FeedItem> lstFeed){ 
        set<id> relatedCasesIds = new set<id>();
        for(FeedItem fd : lstFeed) {
            if (fd.type =='TextPost')relatedCasesIds.add(fd.ParentId);
        }
        Map<Id, case> relatedCases = new Map<Id, case> ([Select id, caseNumber,All_Users_Feed_Count__c FROM case where id in: relatedCasesIds]);
       	List<case> casesToUpdate = new List<case>(); 
        
        integer count = 0;
        for(case c:relatedCases.values()) {
           for(FeedItem fd : lstFeed) {
               if (fd.type =='TextPost' && fd.Visibility =='AllUsers' && fd.ParentId == c.id) {
                   count+=1;
                   
               } 
       		 } 
            c.All_Users_Feed_Count__c = count;
            count = 0;
            casesToUpdate.add(c);
              
        }
        if(casesToUpdate.size() > 0) update casesToUpdate; 
    }
}
Trigger:
trigger FeedItemTrigger on FeedItem (after insert,before delete) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
          FeedItemTriggerHandler.countFeedItem(Trigger.new);    
        }
    } else if(Trigger.isBefore){
      if(Trigger.isDelete){
          FeedItemTriggerHandler.countFeedItem(Trigger.old);    
        }
    }
}



 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Hermann,

Is the value being the same or is it changing to any other value??Also, can you try checking if the flow is going into the loop,because I tried debugging a statement inside but I was not able to print it.

Regards,
Anutej
Hermann Oure 6Hermann Oure 6
Hello ANUTEJ,
When I create a case the field All_Users_Feed_Count__c counts 1. But when I add a text post in the case feed. The field All_Users_Feed_Count__c doesn't increment.
What I am trying to do is anytime there is a text post from the case feed. The count increases on the field All_Users_Feed_Count__c.
But currently it remains to 1. 
And when I make a text post to internal users, the field All_Users_Feed_Count__c goes back to 0.
Not sure how to fix it.
 
ANUTEJANUTEJ (Salesforce Developers) 
I found this thread https://success.salesforce.com/answers?id=9063A000000pUSvQAM where in there is a similar usecase.

 
Praveen ChoudharyPraveen Choudhary
I found this thread Asktop10 (https://asktop10.com/)
Jigar Trivedi 3Jigar Trivedi 3
Hi Hermann & Team,

This might be late reply but still giving apex class code about how I think it should be:
 
public class FeedItemTriggerHandler {

    public static void countFeedItem(List<FeedItem> lstFeed){ 

        Map<Id, Integer> caseIdAndFeedCountMap = new Map<Id, Integer> ();
        for(FeedItem fd : lstFeed) {
            if (fd.type == 'TextPost' && fd.Visibility == 'AllUsers' ){ 
                if(caseIdAndFeedCountMap.containsKey(fd.ParentId)){
                    Integer currentFeedCountTemp = caseIdAndFeedCountMap.get(fd.ParentId);
                    caseIdAndFeedCountMap.put(fd.ParentId, currentFeedCountTemp + 1);
                } else {
                    caseIdAndFeedCountMap.put(fd.ParentId, 1);
                }
            }
        }
        
        if(!caseIdAndFeedCountMap.isEmpty()){
            List<case> getCaseList = [SELECT id, All_Users_Feed_Count__c FROM case WHERE id IN :caseIdAndFeedCountMap.keySet()]; 
        
            for(case caseRecTemp : getCaseList){
                if(null == caseRecTemp.All_Users_Feed_Count__c){
                    caseRecTemp.All_Users_Feed_Count__c = 0;
                }
                caseRecTemp.All_Users_Feed_Count__c = caseRecTemp.All_Users_Feed_Count__c + caseIdAndFeedCountMap.get(caseRecTemp.Id); // or you can create a new Case record and assign value to it, add to list and later at the end update the list separately
            }
            
            if(!getCaseList.isEmpty()){
                update getCaseList;                 
            } 
        }
    }
}

Hope it help someone :)