You need to sign in to do that
Don't have an account?
Hermann 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
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; } }
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
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);
Hope this helps!
Thanks
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
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
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