function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wixxeywixxey 

Invalid field Email for SObject AggregateResult

Hey Plz chk the Error i want to display the AggregateResult on my Visualforce page but it is generating Error " Invalid field Email for SObject AggregateResult" the code is given below

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);
    }
}

 and Visual Force code is

<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>

 

sushant sussushant sus

hi,

 this AggregateResult[] con will provide list of aggregate result so it not possible to bind in vf page

 

More reference : refer visualforce page developer guide

 

Dynamic Visualforce binding is supported for standard and custom objects. Dynamic bindings take the following general form:
reference[expression]
where
• reference evaluates to either an sObject, an Apex class, or a global variable
• expression evaluates to a string that is the name of a field, or a related object. If a related object is returned, it can be used
to recursively select fields or further related objects.
Dynamic bindings can be used anywhere formula expressions are valid. Use them on a page like this:
{!reference[expression]}
Optionally, you can add a fieldname to the end of the whole dynamic expression. If the dynamic expression resolves to an
sObject, the fieldname refers to a specific field on that object. If your reference is an Apex class, the field must be public
or global. For example:
{!myContact['Account'][fieldname]}

 

sushant

 

 

Discuss with meDiscuss with me

here You Are Creating an array of "AggregateResult " But in "con" trying to strore data of Contact object

Modify in either one. Your problem will resolve

 

 

public with sharing class searchDuplicate {

                public List<contact > con {get; set;}                               //  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);

                 }

}

wixxeywixxey
@Disscus with me now the error is
Error: Compile Error: Illegal assignment from LIST<AggregateResult> to LIST<Contact> at line 15 column 30
NanthaNantha

Hi Wixxey.,

 

you can go with AggregateResult itself.,

 

but after getting results  create list for the contact and loop through the AggregateResult and add it to contact list. now you can show it in the Visualforce page

 

List<contact> lstcontact= new List<contact>();
for (AggregateResult ar: con)
{
contact objcontact = new contact(ar);
lstcontact.add(objcontact);
}

 

you can use lstcontact in the visualforce page to show the result

 

please mark as solution if it satisfied you requirement.,

CAD AdministratorCAD Administrator
Hi Wixxey,

Even though your question is over a year old, I want to put the answer here, JIC it helps someone else.

From what I've found, this is a bug in SF.  If you have both the "value" and "headerValue" fields set on an apex:column, it will try to validate against the AggregateResult, which will cause an error.

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

Into this - Notice how I have moved the "value" field to the inner element:
<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>

Thanks,
Michael
mani mohan madasumani mohan madasu
Thank you michael it helped a LOT
Blake FridmanBlake Fridman
Michael, thank you for your reply!
Rohit WardoleRohit Wardole
Michael, thank you so much!