[英]salesforce cast from sObject to custom object
I have written a Base controller that I want to use to manage data pagination on sever controllers.我写了一个 Base controller ,我想用它来管理服务器控制器上的数据分页。
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.然后我的每个扩展基础 controller 的控制器实现自己的 getPagedData 版本。 But return a specific customer object eg
Foo__c
但返回特定客户 object 例如
Foo__c
Can I Cast from List<sObject>
to List<Foo__c>
in a visualforce page我可以在 visualforce 页面中从
List<sObject>
转换为List<Foo__c>
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但是我收到一个错误,sObject 不知道
Bar__r
我尝试使用 dataTable 值和 outputText 内部进行 Cast 但它似乎不起作用
I can use dyamic bindings http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf.htm but then how do i do things like我可以使用动态绑定http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf.htm但是我该怎么做
<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正如我收到的错误所说,它需要一个 DateTime object 等
Been there.到过那里。 Unfortunately, there isn't a way to cast objects directly in the visualforce page.
不幸的是,没有办法直接在 visualforce 页面中投射对象。
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.我解决这个问题的方法是将所有分页逻辑以通用形式移动到您的基本 controller 中,然后让子控制器负责将数据转换为您的 visualforce 页面期望的形式。
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.您也可以考虑使用StandardSetController来控制分页。 It works great for custom objects and most standard objects, but not for custom ApexClasses and some standard objects.
它适用于自定义对象和大多数标准对象,但不适用于自定义 ApexClasses 和一些标准对象。 That said you'll still need to cast your result set as it to returns a List from its getRecords() method.
也就是说,您仍然需要将结果集转换为从其 getRecords() 方法返回 List 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.