Chapter 4. Building the Chatter Application and Cleaning the Chatter Data

Until now, we have learned the basics of Salesforce Chatter configuration and customization. In the last chapter, we went through development with Salesforce Chatter. We also learned how to use the Apex code with Salesforce Chatter in different scenarios. In this chapter, we will go through the following topics:

  • The mass follow and unfollow application
  • Cleaning the Chatter data
  • Managing Chatter groups
  • Deactivating a Chatter user
  • Using schedule Batch Apex to clean the Chatter data

To customize this application in Salesforce, knowledge of Apex and Visualforce is required. After completion of this chapter, you will have a clear idea on how to develop an application with Salesforce Chatter. You will also gain an understanding of the various concepts of Chatter data cleaning.

The mass follow and unfollow application

In the previous chapter, we walked you through the way to follow people in Salesforce Chatter; however, the disadvantage of that way is that you may follow or unfollow each user one by one.

There are a few benefits of following someone on Salesforce Chatter:

  • They keep you updated
  • They make things easier for you; you will get all the updated news and changes across the company by following them
  • It is a more collaborative way to work, and it's a great way to get updates on deals and products

Some tips to get more followers are as follows:

  • Include hashtags in your posts to categorize them so that they will reach more people
  • Provide frequent status updates
  • Use the desktop app to stay up-to-date easily
  • Use the Chatter mobile app for updating statuses, replying to posts, and closing deals

Here, we are introducing an application, Mass follow/Unfollow Chatter users. With this application, you will be able to follow many users at a time. You may unfollow all users, a single user, or users in a bunch. You can customize the functionality of this application as per your requirements.

In the application-landing page, that is the Follow/Unfollow tab, you can see two buttons: Follow and Unfollow. Once you click on any of these buttons, it will redirect you to the respective page. The steps to follow and unfollow users are given in the following screenshot:

The mass follow and unfollow application

In Salesforce, mass follow and mass unfollow have never been such an easy task; but once you have the mass follow/mass unfollow application, it will make things easy for you and save your time. For this application, first create two drop-down lists/picklist fields, that is, role and region. Following are the values for the role and region. You can change the field values according to your requirement:

  • Role: SE, SSE, TL, MANAGER, CTO, and CEO
  • Region: APAC, EMEA, LATAM, and NA

The Mass follow page

To follow someone, click on the Follow button and it will redirect you to the Mass follow page. It will look like the following screen:

The Mass follow page

There is a search panel where you can search for the desired users; you can also customize the search panel according to your requirements, such as adding or removing fields from this panel. Here, you can define the criteria that the user must match in the search result. In addition, you can select as many users as you want to follow. As soon as you follow them, they are removed from the current list. The following is the search-result screen:

The Mass follow page

Developing an application

Now let us come to the development part. Before starting with development, gather all the details about the application. Once this is done, break this requirement in to small parts and start its development.

The following are the key points to understand this application:

  1. Get the list of users that are not being followed by the logged-in user by using the following code:
     eseid= new List<id>();//To store list of users id that is following by current user
    for(EntitySubscriptionese:[select parentid from entitysubscription where subscriberId=:userinfo.getUserid()]) //return list of users that is followed by current user
    {
    followId.add(ese.parentid);// Parentid is id of users i.e followed by current user
                      }
    userSel = [select id from user where id <>: eseid];// This query will return users i.e. not following by current user
  2. To follow users in Chatter, use the following code:
    for (User UnSelectUsers:selectedUsers){
    EntitySubscription follow = new EntitySubscription ();
    follow.parentId = UnSelectUsers.id;// Add the id of user (Parentid) whom you want to follow
    follow.subscriberid = UserInfo.getUserId();//Subscriberid is store id of user who is going to follow
    UsersToInsert.add(follow);// Add all selected user in a list
       }
    Database.insert(UsersToInsert );// Insert all records from list

    Note

    The EntitySubscription object stores information about user/records that are being followed in Chatter. A user can subscribe to the record as well as to the users.

The code mentioned in the second point will be responsible for enabling users to follow other users/records. Apart from this, you will also be able to customize the output panel and remove or add field(s) according to your requirements.

The Mass unfollow page

To unfollow a single user or multiple users, navigate to the Follow/Unfollow tab, and click on the Unfollow button, as shown in the following screenshot:

The Mass unfollow page

It will redirect you to the Mass Unfollow page and a window with a list of users that you are following will appear (shown in the following screenshot):

The Mass unfollow page

There are two ways to unfollow users:

  1. You can select as many users as you want from the given list and click on the Unfollow button.
  2. You can unfollow all users by clicking on the Unfollow All button.

The following are the key points to understand this application:

  1. The list of users being followed by the logged-in user can be obtained using the following code:
    public List<cEntitySubscription>getEntitySubscriptions() 
    {
    if(EntitySubscriptionList == null) 
    {
    EntitySubscriptionList = new List<cEntitySubscription>();
                String userSObjectPrefix=  User.sObjectType.getDescribe().getKeyPrefix();// To get user object prefix
    for(EntitySubscription c: [select id, parentid, subscriberid, parent.name  from EntitySubscription where subscriberid =:uid order by Parentid])//This query will give list of user's and records following by logged In user
     {
    if( ('' + c.parentid).substring(0,3) == userSObjectPrefix) // users only, by this you can eliminate records
                                  {
    EntitySubscriptionList.add(new cEntitySubscription(c));// Add user in EntitySubscriptionList
                                   }
                }
            }
    returnEntitySubscriptionList;
        }
  2. The following code explains how to unfollow the selected user(s):
    selectedEntitySubscriptions = new List<EntitySubscription>();
    // this list store records selected by user for unfollow
    
    for(cEntitySubscriptioncCon: getEntitySubscriptions()) {
    // getEntitySubscriptions() store list of following by current user, for more info see point 1
    
    if(cCon.selected == true) {
    // If users is selected then add in list
    
    selectedEntitySubscriptions.add(cCon.con);
    }}
    delete selectedEntitySubscriptions;
    //once you have list of user that are Logged in  and if the user want to unfollow, use delete command and delete list.
    

    When you select a user and click on the Unfollow button, the user will be removed from the list of users that you are following and your list of followers as well.

  3. The following code explains how to unfollow all users:
    selectedEntitySubscriptions = new List<EntitySubscription>();
    
    for(cEntitySubscriptioncCon: getEntitySubscriptions()) {// Add list ofuser following by current use rin a list
    
    selectedEntitySubscriptions.add(cCon.con);     
    }
    delete selectedEntitySubscriptions; //once you have list of user that are Logged in user want to unfollow, use delete command and delete list.

    Note

    Only example snippets are provided here. However, you can download the complete code from www.packtpub.com/support.

Now follow and unfollow any user whenever you want using the Mass follow/unfollow feature and save your time.

Steps to install the mass follow/unfollow Chatter application

The following steps explain the installation process of the mass follow/unfollow Chatter application:

  1. Download code and images from the attached CD.
  2. Upload the images 1164EN_04_01 and 1164EN_04_02 in static resource named Follow and Unfollow respectively.
  3. Create two fields Role and Region in the User object with the following details:
    • Role: SE, SSE, TL, MANAGER, CTO, and CEO
    • Region: APAC, EMEA, LATAM, an NA
  4. Create the Visualforce pages with names for the follow page (MassFollowPage) and unfollowpage (MassUnfollowPage), and add supporting classes from the ZIP file that you downloaded from CD.
  5. The MassFollowUnfollow Visualforce page can be found under the Visualforce tab.
  6. While creating the Visualforce tab, you have to select the MassFollowUnfollow page in the Visualforce page dropdown.

Tasks

Some tasks that you should perform are as follows:

  • Write Apex code to check the number of users following by user; if it reaches more then 500 (as per the Salesforce governor limit) print error in MassFollowPage.
  • Implement pagination in MassFollowPage and MassUnfollowPage.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.2.223