Custom Chatter Dashboard Component | Salesforce


In this blog I will create a custom visualforce page to display list of Users and count of chatter post by individual user. I will be creating a flag also which will turn Red if user has posted less than two posts in a week and if the post count for the week is two or more than two than it will display Green flag there.

Visualforce Page

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<apex:page controller="ChatterWeeklyActivityCtrl" lightningStylesheets="true" >
    <apex:slds />
    <br/>
    <br/>
    <b> <apex:image value="/img/samples/flag_green.gif" rendered="{!showTick}"/><apex:image value="/img/samples/flag_red.gif" rendered="{!!showTick}"/> You have contributed {!feedCount} posts in this week !</b>
    <br/>
    <br/>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!wfList}" var="w" id="table2" title="Users Feeds">
            <apex:column value="{!w.userName}" headerValue="User Name"/>
            <apex:column value="{!w.posts}" headerValue="Weekly Posts"/>
            <apex:column headerValue="Status" >
                <apex:image value="{!w.flag}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


Apex

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class ChatterWeeklyActivityCtrl {
    public List<FeedItem> feedsList{get;set;}
    public Integer feedCount{get;set;}
    public Boolean showTick{get;set;}
    public List<wrapFeeds> wfList{get;set;}
    public ChatterWeeklyActivityCtrl(){
        wfList = new List<wrapFeeds>();
        feedCount=0;
        List<User> userList = [SELECT Id,Name FROM User WHERE isActive=TRUE AND (Profile.Name='System Administrator' OR Profile.Name='Work.com Only User' OR Profile.Name='Custom: Sales Profile') ORDER BY Name];
        Map<String,List<FeedItem>> userFeedMap = new Map<String,List<FeedItem>>();
        feedsList=[SELECT Title,Id, Type, CreatedBy.Name, CreatedById, CreatedDate FROM FeedItem WHERE 
                   (Type ='TextPost' OR Type='QuestionPost') AND 
                   CreatedDate= This_Week];
        if(feedsList != null && feedsList.size()>0){
           
                for(FeedItem fi:feedsList){
                    List<FeedItem> resultFi = userFeedMap.get(fi.CreatedById);
                    IF(resultFi == null){
                        resultFi = new List<FeedItem>();
                    }
                    resultFi.add(fi);
                    userFeedMap.put(fi.CreatedById,resultFi);
                }
            
        }
        for(User u:userList){
            if(userFeedMap.containsKey(u.Id)){
                if(u.Id == UserInfo.getUserId()){
                    feedCount = userFeedMap.get(u.Id).size();
                }
                wfList.add(new wrapFeeds(u.Name,userFeedMap.get(u.Id).size()));
                System.debug('###'+u.Name+' : '+userFeedMap.get(u.Id).size());
            }
            else{
                wfList.add(new wrapFeeds(u.Name,0));
                System.debug('###'+u.Name+' : 0');
            }
        }
        
        if(feedCount >=2){
            showTick = true;
        }
        else{showTick=false;}
    }
    Public class wrapFeeds{
        public String userName {get;set;}
        public Integer posts {get;set;}
        public String flag{get;set;}
        public WrapFeeds(String user, Integer postCount){
            userName = user;
            posts = postCount;
            if(posts >=2){
                flag='/img/samples/flag_green.gif';
            }
            else{
                flag='/img/samples/flag_red.gif';
            }
        }
    }
}


Test Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@isTest
public class ChatterWeeklyActivityCtrlTest {
    static testMethod void testChatter(){
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        Account obj = new Account(Name='Test Account');
        insert obj;                                              
        f.parentid = obj.id;  
        insert f;
        ChatterWeeklyActivityCtrl ca = new ChatterWeeklyActivityCtrl();
    }
}


Watch Complete video below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 

Post a Comment

0 Comments