0

I created a Apex class :

public class HellowWorld{
    public List<Contact> getContacts() {    
        List<Contact> contacts = Database.query('select a,b,c,d,e,f,g,h,i from alphabet');
        return contacts;  
    }
}

Which the developer console accepts without problems/issues.

Then I try to use this in a force page :

<apex:page controller="HellowWorld">
    <apex:form >
        <apex:pageBlock > 
        <apex:pageBlockTable value="{! contacts}" var="ct">
        <apex:column value="{! ct.a}"/>
        </apex:pageBlockTable>
        </apex:pageBlock>   
    </apex:form>  
</apex:page>

I then receive the following error on thepage preview :

Invalid conversion from runtime type List to List

Is anyone able to assist?

  • can you return list contact from your apex class? – RedDevil Apr 10 '19 at 6:50
  • At runtime Database.query('select a,b,c,d,e,f,g,h,i from alphabet'); returns the List<alphabet> not List<Contact>. At compile time, the engine doesn't know exact type of the return value. That's why it saved your code, but failed when you ran the page. – Eduard Apr 10 '19 at 7:16
2

You are getting this error because, you are querying on alphabet object and assigning it to List<Contact>. And as you are using dynamic SOQL, you will get run time exception which is expected.

If you want to display alphabets in your visualforce page, then your controller should be something like below

public List<alphabet> getAlphabets() {    
    List<alphabet> alphabets = Database.query('select a,b,c,d,e,f,g,h,i from alphabet');
    return alphabets;  
}

Instead of using the dynamic SOQL, you can simply use as below

public List<alphabet> getAlphabets() {    
    return [select a,b,c,d,e,f,g,h,i from alphabet];
}

And your visualforce page should be

<apex:page controller="HellowWorld">
    <apex:form >
        <apex:pageBlock > 
        <apex:pageBlockTable value="{!alphabets}" var="ct">
        <apex:column value="{! ct.a}"/>
        </apex:pageBlockTable>
        </apex:pageBlock>   
    </apex:form>  
</apex:page>
  • Thank you Vijay that worked as a charm. – Edward Nigma Apr 10 '19 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.