2

I am trying to implement functionality in SalesForce where when a user clicks a button then the following count increases.

Currently I have a simple test page that just shows the following count. When a user clicks a commandbutton they follow a new user and the following count gets increased by one. This all works OK on the back end. The followingCount gets increased by one.

The issue is that when I click the button my page appears to reload but the followingCount on the screen is not the new updated number but the old stale value.

Example: if the followingCount is 1 and I click the button, the followingCount in the back end is correctly increased to 2, however when the page reloads it still displays on the page as 1.

When I manually refresh the page myself the count is then displayed correctly as 2.

Here is my code:

VisialForce Page

 //Section that holds some data is a list
 <div class="ui-block-c text">{!followingCount}</div>

 //Apex button
 <apex:commandButton id="CommandButton1" value="Follow" action="{!followOwner}"/>

APEX Class

  public PageReference followOwner() 
{ 
        User userDetails = GetLoggedinUserDtls();
        User profileUser = getUserDtls();

        EntitySubscription follow = new EntitySubscription (); 
        follow.parentId = UserId;
        follow.subscriberid = UserInfo.getUserId();
        insert follow; 

    return null;         
} 

So I'm guessing that when I click the command button my visualforce page isn't getting fully reloaded? I have tried to use rerender with the command button but it didn't make any difference. Can anybody suggest why this might be happening?

Adding code for following count:

//Variable
public Integer followingCount{get;set;}

//Constructor
public userData (ApexPages.StandardController stdController) {
    UserId = UserInfo.getUserId();
    GetCount();
}

 //Method
 public void GetCount() {

 List<User> following = [SELECT Id, Name,title, SmallPhotoUrl FROM User WHERE Id =:parentsetid AND Id !=:u.id order by name]; 
    followingCount = following.size();
 }
  • Can you add the code for followingCount as well. The issue could be in how the count is done. – BarCotter Jul 28 '14 at 10:56
  • Just done. Supplied the code for the following count. – Donal Rafferty Jul 28 '14 at 11:01
3

The problem is because GetCount is only called when the page loads and never called again when the button is pressed. You could change followingCount to the code below so that the SOQL gets re-queried each time it is used.

public Integer followingCount{
    get {
        List<User> following = [SELECT Id, Name,title, SmallPhotoUrl FROM User WHERE Id =:parentsetid AND Id !=:u.id order by name]; 
        return following.size();
    }
}

Another solution would be to call GetCount at the end of the followOwner action

2

It looks like you might need to requery the count after inserting the new Entity Subscription.

I would suggest using ConnectApi for this. If you had an integer that showed this information, you could do this:

public Integer uFollowCount {get;set;}

public PageReference followOwner() 
{ 
    User userDetails = GetLoggedinUserDtls();
    User profileUser = getUserDtls();

    EntitySubscription follow = new EntitySubscription (); 
    follow.parentId = UserId;
    follow.subscriberid = UserInfo.getUserId();
    insert follow; 

    ConnectApi.UserDetails udetail = (ConnectApi.UserDetail) ConnectApi.ChatterUsers.getUser(UserId);
    uFollowCount = udetail.followingCounts.people; 

    return null;         
} 

Alternatively, you could just write this into the getter for the uFollowerCount property. I would also look into using the connectapi for doing the following as well with the ConnectApi.ChatterUsers.follow() method.

It occurred to me that there exists the possibility that we are updating the following counts asynchronously. If that is the case, you may still experience lag in the count updating regardless of getting the updated value as above.

Note: Code untested, this is based on free-hand coding and reading of the docs, so it is very likely I've made a mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.