You need to sign in to do that
Don't have an account?
System.QueryException: List has more than 1 row for assignment to SObject
I have been following the cookbook and want to display a list using visualforce of all the accounts that are applying and awaiting 2 screenings. I have tested the code using apex explorer and this apex returns a list of sobjects. But when I try to move through them using visual force I get the following error. How can I rewrite this code to display all the names?
<apex:pageBlock title="Select a Candidate">
<apex:dataTable value="{!account}" var="u" cellPadding="4" border="1">
<apex:column >{!u.name}</apex:column>
<apex:column >{!u.id}</apex:column>
</apex:dataTable>
</apex:pageBlock>
public Account getaccount() {
return [select id, name from Account where Recruitment_status__pc = 'Applying' AND Application_sub_stage__pc = 'Awaiting 2 screenings'];
}
The getAccount method in your controller returns a single Account object. But the SOQL query is returning more than 1 record.
So change the getAccount method to return a List:
public List<Account> getaccount() { return [select id, name from Account where Recruitment_status__pc = 'Applying' AND Application_sub_stage__pc = 'Awaiting 2 screenings']; }
All Answers
The getAccount method in your controller returns a single Account object. But the SOQL query is returning more than 1 record.
So change the getAccount method to return a List:
public List<Account> getaccount() { return [select id, name from Account where Recruitment_status__pc = 'Applying' AND Application_sub_stage__pc = 'Awaiting 2 screenings']; }
I had the same issue few days ago. The problem is with the function "getaccount()" This should return one record i.e. one Account record. Your SOQL may be returning multiple records and the return type one Account cannot carry multiple records.
You need to finetune the SOQL.
Edit: The above post gives perfect solution, as you are iterating the results in the Visual Force page.
Dear Aalbert,
I refer to your reply below on solving a System.QueryException. My public list SOQL query is working fine but I have a similar error message when I write a test method to test this public list SOQL query. Please could you advise me how we should be writing the test method correctly in order to avoid having such an error message? Hope you will be able to advise me with a practical example based on the sample public list SOQL query in your reply. This will be most helpful as I have not been able to resolve this despite various methods.
Sorry for any inconvenience caused.
Thanks and Best Regards
SL