开发者

salesforce cast from sObject to custom object

开发者 https://www.devze.com 2023-03-09 23:19 出处:网络
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

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 implement 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]}" />
  </apex:column>   

But I get an error that sObject does not have 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 开发者_StackOverflowdyamic bindings http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf.htm but then how do i do things like

<apex:outputText value="{0, number, $###,###}">
         <apex:param value="{!c.Amount__c}" />
</apex:outputText>
<apex:outputText value="{0,date,dd/MM/yyyy}">
          <apex:param value="{!c.Date_Of_Birth__c}" />
</apex:outputText>   

As i get errors as saying it expects a DateTime object etc


Been there. Unfortunately, there isn't a way to cast objects directly in the visualforce page.

The way I've addressed this 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号