VisualForce中sobjectaggregateresult无效的字段Email



我想在我的Visualforce页面上显示AggregateResult,但它正在生成以下错误" Invalid field Email for SObject AggregateResult"

下面是我的代码:
public with sharing class searchDuplicate {
public   AggregateResult[] con{get;set;}
public searchDuplicate()
{
    find();
}
public void find(){
   con = [select Email from Contact group by Email having count(Email) > 1];
    System.debug(con);
}
}
下面是我的visualforce页面代码:
<apex:page controller="searchDuplicate">
<apex:pageBlock title="Searching for Duplicate Contacts Record"> 
</apex:pageBlock>
<apex:pageBlock title="Contacts">
    <apex:dataTable value="{!con}" var="c" border="2" cellspacing="5" cellpadding="5">
        <apex:column headerValue="Email" value="{!c['Email']}" />
    </apex:dataTable>
</apex:pageBlock>     
</apex:page>

如果可能的话请更正

public with sharing class searchDuplicate {
public  list <con> conList{get;set;}
public class con{
   public string Email {get;set;}
   public con( string email){
      this.Email = email;
   }
}
public searchDuplicate()
{
    find();
}
public void find(){
conList = new  list <con>();  
for( AggregateResult ar : [select Email from Contact group by Email having count(Email) > 1];){ conList.add(new con(string.valueOf(ar.get('Email'))))  } 
}
}

聚合结果(及其字段/列)作为泛型对象出现,而不是sobject。因此没有obj.get('somefieldname')可以调用。

您最好的选择可能是创建一个具有String email; Integer count;字段的helper类,并循环遍历填充该helper类的对象列表的查询结果。

您也可以使用Map<String, Integer>,但这会使VF没有按字母顺序排序。

如果需要代码示例,请参阅https://salesforce.stackexchange.com/questions/7412/count-a-grouped-by-soql。

虽然其他答案对解决你的问题是正确的,但你所得到的实际上是SalesForce的一个错误。

在不创建自定义对象的情况下解决此问题的方法是将其分为两个- "headerValue"one_answers"value"都被设置导致此错误。

你想把它改成这样:

<apex:dataTable value="{!con}" var="c" border="2" cellspacing="5" cellpadding="5">
    <apex:column headerValue="Email" >
        <apex:outputText>{!c['Email']}</apex:outputText>
    </apex:column>
</apex:dataTable>

谢谢,迈克尔。

最新更新