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
Shanke_PaudelShanke_Paudel 

Sowing more than one row in apex:pageBlock

My Custom Visualforce page.
<apex:page controller="UnapprovedList">
<apex:pageBlock title="Opportunities">
<apex:pageBlockTable value="{!Opportunity}" var="item">
<apex:column value="{!item.Name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

My Controller
public class UnapprovedList{
private final Opportunity opportunity;

public UnapprovedList() {
opportunity = [SELECT Id, Name FROM Opportunity limit 1];
// the page displays error if i remove limit 1.
//System.QueryException: List has more than 1 row for assignment to SObject
}

public Opportunity getOpportunity () {
return opportunity;
}
}

where is the problem?? i want to show more than one row but when i return multiple row the error pops up..
Any help would be appricated.

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Use below code, error will be resolved.

 

public class UnapprovedList{
	//private final Opportunity opportunity;
	Public List<Opportunity> opportunity{get;set;}
	public UnapprovedList() {
		opportunity = [SELECT Id, Name FROM Opportunity];
		// the page displays error if i remove limit 1.
		//System.QueryException: List has more than 1 row for assignment to SObject
	}

	public Opportunity getOpportunity () {
		return opportunity;
	}
}

 

All Answers

Dhaval PanchalDhaval Panchal

Use below code, error will be resolved.

 

public class UnapprovedList{
	//private final Opportunity opportunity;
	Public List<Opportunity> opportunity{get;set;}
	public UnapprovedList() {
		opportunity = [SELECT Id, Name FROM Opportunity];
		// the page displays error if i remove limit 1.
		//System.QueryException: List has more than 1 row for assignment to SObject
	}

	public Opportunity getOpportunity () {
		return opportunity;
	}
}

 

This was selected as the best answer
Puja_mfsiPuja_mfsi

Hi,

if you want to access more than one row then you need to use List to get the results.

i.e if u need to get opportunity records then u need to create a variable of List<opportunity> not opportunity.

 

private final Opportunity opportunity; 

replace it with 

List<Opprtunity> opptyList;

public UnapprovedList() {
        opptyList = [SELECT Id, Name FROM Opportunity]; // Now it will not give any error.
}

 

 

Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.