You need to sign in to do that
Don't have an account?
Kondal Rao learner
how to display the reocrds in tableformat
Hi Experts,
Im very new to salesforce so when i write the program in salesforce , so when i displaying the records in tabluar format but it showing some error "
Visualforce Error
System.QueryException: List has no rows for assignment to SObject
Class.mySecondController.getAccount: line 5, column 1 "
please check my code once ,
public class mySecondController { public Account getAccount() { return [select id, name, (select id, firstname, lastname from Contacts limit 5) from Account where id =:System.currentPageReference().getParameters().get('id')]; } public String getName() { return 'My Second Custom Controller'; } }
visual force page:
<apex:page controller="mySecondController" tabStyle="Account"> <apex:pageBlock title="Hello {!$User.FirstName}!"> You belong to the {!account.name} account. </apex:pageBlock> <apex:pageBlock title="Contacts"> <apex:dataTable value="{!account.Contacts}" var="contact" cellPadding="4" border="1"> <apex:column > {!contact.FirstName} </apex:column> <apex:column > {!contact.LastName} </apex:column> </apex:dataTable> </apex:pageBlock> </apex:page>
Your problem is here:
While this is perfectly legal, it is a quite dangerous practice with Apex.
When you execute a query and pass the results into a single sObject, Apex will throw an exception if your query returns anything other than exactly one row. Since your getter returns Account, when your query is invoked and returns zero rows, this is the exception you are seeing. And by returning the results of the query directly, there is no way to catch the exception. You need to do something like this:
This is by no means the only way to do this. You could also select into a list, check for exactly one member and then return that. Different developers have different techniques. But don't ever query into a single sobject with out some way to handle the potential exception.
i've also promoted account to a class member variable. This is typical, but carries the overhead of filling the account record into the view state. I'm not sure if that's what you're trying to avoid in your original code, but if you didn't want that, you could simply use the transient keyword in front of the attribute declaration to remove it from the view state.
All Answers
use apex:pageblocktable table instead of apex:dataTable
Your problem is here:
While this is perfectly legal, it is a quite dangerous practice with Apex.
When you execute a query and pass the results into a single sObject, Apex will throw an exception if your query returns anything other than exactly one row. Since your getter returns Account, when your query is invoked and returns zero rows, this is the exception you are seeing. And by returning the results of the query directly, there is no way to catch the exception. You need to do something like this:
This is by no means the only way to do this. You could also select into a list, check for exactly one member and then return that. Different developers have different techniques. But don't ever query into a single sobject with out some way to handle the potential exception.
i've also promoted account to a class member variable. This is typical, but carries the overhead of filling the account record into the view state. I'm not sure if that's what you're trying to avoid in your original code, but if you didn't want that, you could simply use the transient keyword in front of the attribute declaration to remove it from the view state.
HI,
Thanks all for the help and ur msgs.