简体   繁体   English

salesforce 从 sObject 转换为自定义 object

[英]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.

相关问题 我们可以在 Salesforce(apex) 中动态转换 SObject 吗? - Can we Cast SObject dynamically in Salesforce(apex)? Salesforce:从Apex访问Document sObject中的文档 - Salesforce: Accessing Documents in Document sObject from Apex 我可以在 Salesforce 平台事件中创建数据类型为 sobject 的自定义字段吗? - Can I create a custom field with datatype as sobject in the Salesforce platform event? .Net Salesforce集成-从列表中获取列值<sObject> - .Net Salesforce integration - getting column values from List<sObject> Camel-salesforce Sobject错误 - Camel-salesforce Sobject error SalesForce-来自另一个自定义对象的总和记录 - SalesForce - Sum records from another custom object Salesforce - 从自定义对象中的标准对象引用自定义字段 - Salesforce - Refer custom field from standard object in custom object Webshpere铸铁Salesforce集成无法从Salesforce检索AccountContactRelation对象 - Webshpere Cast Iron Salesforce integration Unable to retrieve AccountContactRelation object from Salesforce 自定义object查找关系soql查询获取sobject - custom object lookup relationship soql query to get sobject Salesforce - 错误“DML 需要 SObject 或 SObject 列表类型:帐户” - Salesforce - Error "DML requires SObject or SObject list type: account"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM