+ Start a Discussion
Hermann Oure 6Hermann Oure 6 

Iterating over a Map With a for loop

Hello,

I have created an apex class to count the number of textpost in a case but my code doesn't not iterate...
Could someone help?
Thanks
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; 
    }
}

 
Best Answer chosen by Hermann Oure 6
SUCHARITA MONDALSUCHARITA MONDAL
Hi Hermann,
Set you count as 'Static'.  I think count is incrementing/scoped in inner loop, once it's in outer loop it becomes 0 (as per initialization).
Defining count variable as static will keep only one copy and you'll get incremented value.

P.S. - Always put debug to analyse the flow and values of variable.
System.debug('--count--'+count);

Thanks,
Sucharita

All Answers

SarvaniSarvani
Hi Hermann,

The code works fine for me. The code does count the feed item records(Insert Trigger) on case record of type "TextPost" with visibility "AllUsers" and counter increments and updates value in All_Users_Feed_Count__c field of case record. As per your code the count increments only if the conditions are satisfied so make sure your feed item is satisfying all the values in the If condition.

Try executing the below code in your anonymus window and check if your trigger/class fired and check for debug logs if the field is updated with value 2. If you do not have trigger yet but testing with only apex class. You have to explicitly call your class as in line 13 with list of feed items like FeedItemTriggerHandler.countFeedItem(listfeed);
Case c=new case();
c.subject='Test Today 12345';
c.Status='new';
c.origin='Phone';
insert c;
System.debug(c.id);
list<feeditem> listfeed= new list<feeditem>();
FeedItem f=new feeditem(Parentid=c.id,Body='Feed item1111', type='TextPost', Visibility ='AllUsers');
listfeed.add(f);
FeedItem f1=new feeditem(Parentid=c.id,Body='Feed item2222', type='TextPost',Visibility ='AllUsers');
listfeed.add(f1);
insert listfeed;
FeedItemTriggerHandler.countFeedItem​​​​​​​(listfeed); // optional line if only class is implemented and its not called by any trigger.
System.debug(listfeed);
System.debug(c.All_Users_Feed_Count__c);

Hope this helps!

Thanks
SUCHARITA MONDALSUCHARITA MONDAL

Hi Hermann Oure 6,

Your code won't iterate as 'fd.Visibility'  condition will not match. 
Try to apply the condition on Line 5 ..as below

set<id> relatedCasesIds = new set<id>();
        for(FeedItem fd : lstFeed) {
            if (fd.type =='TextPost' && fd.Visibility =='AllUsers')
                relatedCasesIds.add(fd.ParentId);
        }

Also, you don't have to use Map (Line 7) and second For loop (Line 12) . You can use simple List on Line 7 as SOQL is going to fetch only those cases where condition (if (fd.type =='TextPost' && fd.Visibility =='AllUsers')) will be matched.

Let me know your thoughts,


Thanks,
Sucharita

 

 

Hermann Oure 6Hermann Oure 6
Hi Sucharita,
Thanks for your answer.
I have tried what you suggested on Line 5 but still, my field does not incremente count.
Is there something else, I should change?
Thanks,
Hermann
SUCHARITA MONDALSUCHARITA MONDAL
Hi Hermann,
Set you count as 'Static'.  I think count is incrementing/scoped in inner loop, once it's in outer loop it becomes 0 (as per initialization).
Defining count variable as static will keep only one copy and you'll get incremented value.

P.S. - Always put debug to analyse the flow and values of variable.
System.debug('--count--'+count);

Thanks,
Sucharita
This was selected as the best answer