Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

I have written a Base controller that I want to use to manage data pagination on sever controllers.

I have an abstract method like so:

 public abstract List<sObject> getPagedData();

Then each of my controllers that extend the base controller implements their own version of getPagedData. But return a specific customer object e.g Foo__c

Can I Cast from List<sObject> to List<Foo__c> in a visualforce page

My page looks like this:

<apex:dataTable value="{!PagedData}"  var="c"   >

     <apex:column > 

          <apex:facet name="header">Foo</apex:facet>

          <apex:outputText value="{!c.Bar__r.SomeValue__c]}" />


But I get an error that sObject does not have to know about Bar__r I have tried doing a Cast with the dataTable value and inside the outputText but it does not seem to work

I can use dynamic bindings but then how do i do things like

<apex:outputText value="{0, number, $###,###}">

         <apex:param value="{!c.Amount__c}" />


<apex:outputText value="{0,date,dd/MM/yyyy}">

          <apex:param value="{!c.Date_Of_Birth__c}" />


As I get errors as saying it expects a DateTime object etc.

1 Answer

0 votes
by (32.1k points)
edited by

The way I've approached this problem is to move all the pagination logic into your base controller in generic form and then have the child controllers take on the responsibility for casting the data into the form your visualforce page expects.

public List<Foo__c> getFooPagedData() {

    List<Foo__c> fooPagedData = new List<Foo__c>();

    for(SObject record : getPagedData()) {

       fooPagedData.add((Foo__c) record));


    return fooPageData;


You might also consider using the StandardSetController to control your pagination. It works great for custom objects and most standard objects, but not for custom ApexClasses and some standard objects. That said you'll still need to cast your result set as it to returns a List from its getRecords() method.

To learn in-depth about Salesforce, sign up for industry-based Salesforce Training!

Browse Categories