Wednesday, 23 April 2014

Auto-updatable chatter feed in Salesforce

Let’s consider the process of implementation of auto-updatable chatter feed. Even if it looks quite easy this task has two aspects:
1) Auto-updatable controls
2) Creation of custom controllers

In general case auto-refresh of Salesforce visual element can be implemented using this code snippet:

<apex:actionPoller action="{!updateMethod}" reRender="controlToUpdate" interval="15"/>

where:
“updateMethod” is a method of page controller
“controlToUpdate” - id of a component to update
“15” - update interval in seconds

Unfortunately, only custom controllers can have an update method, all standard controllers cannot be extended using ordinary inheritance. So this task may look quite tricky.

Let’s have a look on an example of Account controller. It can be extended using this approach:

public with sharing class Account_Controller {

   public Account_Controller (ApexPages.StandardController stdController) {
       
this.account = (Account)stdController.getRecord();
   
}

   
public Account account { get; private set; }

   
public PageReference updatePage() {
       
return null;
   
}
}

So, it can aggregate the standard controller and extend it with custom functionality.

And in this case the page for the view may look like this:

<apex:page standardController="Account" extensions="Account_Controller">
   
<apex:form >
      
<apex:actionPoller action="{!updatePage}" reRender="chatter" interval="15"/>
   
</apex:form>
   
<chatter:feed id="chatter" entityId="{!account.id}" showpublisher="True"></chatter:feed>
</apex:page>

And now we can see that two tasks are solved: standard controller is extended through adaptor pattern and the view can incorporate an auto-updatable control.

No comments: