Search code examples
salesforceapex-codevisualforce

Salesforce Page auto update upon update of database


I have the following page that shows documents from database, what I'm trying to accomplish is to make this page refresh or update automatically if a new document is added in the database. Is there a way I can use AJAX or pulling or something in my controller or page to accomplish that ?

Page:

    <apex:pageBlockTable value="{!docs}" var="d" rendered="{!NOT(ISNULL(docs))}" Title="Documents">  
        <apex:column headerValue="Name">
               <apex:outputText value="{!d.Name}"/>           
       </apex:column>   
   </apex:pageBlockTable>

Contoller

 public List<FTPAttachment__c> getDocs()
    {
        docs= [Select Name from FTPAttachment__c where Case__c  = :cse.id];
        return docs;

    }

Solution

  • Sounds like you're looking for the <apex:actionPoller> tag:

    <apex:actionPoller action="{!refreshDocs}" rerender="docsTable" interval="5" />
    <apex:pageBlockTable id="docsTable" value="{!docs}" var="d" rendered="{!NOT(ISNULL(docs))}" Title="Documents">  
        <apex:column headerValue="Name">
            <apex:outputText value="{!d.Name}"/>           
        </apex:column>   
    </apex:pageBlockTable>
    

    You could have the refreshDocs() method explicitly repopulate the docs list, but since you're already doing that in your getter (which will be called when the table is re-rendered), this method can just return without doing anything special:

    public List<FTPAttachment__c> getDocs() {
        return [Select Name from FTPAttachment__c where Case__c  = :cse.id];
    }
    public PageReference refreshDocs() {
        return null;
    }