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
Kondal Rao learnerKondal 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>

 

Best Answer chosen by Admin (Salesforce Developers) 
Peter_sfdcPeter_sfdc

Your problem is here: 

 

public Account getAccount() {
return [select id, name, (select id, firstname, lastname from Contacts limit 5) from Account where id = :System.currentPageReference().getParameters().get('id')];
}

 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: 

//class member attribute.
//use transient if you don't want it in the view state. private Account acct; public Account getAccount() { try { //same query as you did earlier acct = [SELECT ...blah blah blah...]; } catch (QueryException e) { //if you don't get exactly one record back, pass back an empty account record. acct = new Account(); } return acct; }

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

Raj_Raj_

use apex:pageblocktable table instead of apex:dataTable 

Peter_sfdcPeter_sfdc

Your problem is here: 

 

public Account getAccount() {
return [select id, name, (select id, firstname, lastname from Contacts limit 5) from Account where id = :System.currentPageReference().getParameters().get('id')];
}

 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: 

//class member attribute.
//use transient if you don't want it in the view state. private Account acct; public Account getAccount() { try { //same query as you did earlier acct = [SELECT ...blah blah blah...]; } catch (QueryException e) { //if you don't get exactly one record back, pass back an empty account record. acct = new Account(); } return acct; }

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. 

This was selected as the best answer
Kondal Rao learnerKondal Rao learner

HI,

 

Thanks all for the help and ur msgs.