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
devloper sfdcdevloper sfdc 

I am facing error on apex class "List has more than 1 row for assignment to SObject"

Hi All,
I  am facing errror when execute this class through vf page  and also it is showing one record instead of more record .
it is show error "List has more than 1 row for assignment to SObject
Error is in expression '{!getData}' in component <apex:commandButton> in page cpqvf: Class.ProductEntry.getData: line 13, column 1
An unexpected error has occurred. Your development organization has been notified.

"
Please help .

public class ProductEntry{ 
    public List<sObjectWrapper> wrappers{get;set;} 
Public string selectedname{get;set;}
 public List<PricebookEntry> PriceEntry{get;set;}
    public  ProductEntry()
    {
    PriceEntry=new List<PricebookEntry>();
    } 
 public void getData()
 {
     List<sObjectWrapper> wrapper=new List<sObjectWrapper>();
     
    product2 productList=[select id,name,Units_of_Measure__c from product2 where Bundle_Type__c=:selectedname];
     PriceBook2 pricebook=[select id from pricebook2 where name In('BlackBeltHelp V2')];
    PriceEntry=[SELECT id,Name,Pricebook2Id,Product2Id,ProductCode,UnitPrice,UseStandardPrice FROM PricebookEntry WHERE Pricebook2Id=:pricebook.id and product2Id=:productList.id];
    }     
    Public List<string> optionList=new List<String>{'Basic','Advance','Primium'};
        Public List<Selectoption> getselectedaccnamefields(){
            List<Selectoption> lstnamesel = new List<selectoption>();
            lstnamesel.add(new selectOption('', '- None -'));
            for(String s :optionList){
            lstnamesel.add(new selectoption(s,s));
            }
            return lstnamesel; 
        }
}
and my viasulforce page is 

<apex:page controller="ProductEntry">
    <apex:form >
        <apex:pageBlock title="Select Product List">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Bundle Type"/>
                    <apex:selectList size="1" value="{!selectedname}"> 
        <apex:selectOptions value="{!selectedaccnamefields}"/>  
    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!PriceEntry}" var="a">
                <apex:column value="{!a.id}"/>
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.Pricebook2Id}"/>
            <apex:column value="{!a.UnitPrice}"/>
                <apex:column value="{!a.ProductCode}"/>
                <apex:column value="{!a.UseStandardPrice}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="GetData" action="{!getData}"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
Best Answer chosen by devloper sfdc
Shivdeep KumarShivdeep Kumar
Hi, 
Please try below code..
public void getData()
 {
     Set<Id> productIds = New Set<Id>();
for(Product2 p : [select id,name,Units_of_Measure__c from product2 where Bundle_Type__c=:selectedname]){
productIds.add(p.Id);
}     
     PriceBook2 pricebook=[select id from pricebook2 where name In('BlackBeltHelp V2')];
    PriceEntry=[SELECT id,Name,Pricebook2Id,Product2Id,ProductCode,UnitPrice,UseStandardPrice FROM PricebookEntry WHERE Pricebook2Id=:pricebook.id and product2IdIN:productIds];
    }

Please let me know if this help!

Thanks
Shivdeep

All Answers

Raj VakatiRaj Vakati
Change your line 13  . select id,name,Units_of_Measure__c from product2 where Bundle_Type__c=:selectedname SOQL is returning list of records . So update the code to 
 
product2 productList=[select id,name,Units_of_Measure__c from product2 where Bundle_Type__c=:selectedname Limit 1];

 
devloper sfdcdevloper sfdc
Hi Raj ,
But i need to disply more than one record 
Shivdeep KumarShivdeep Kumar
Hi, 
Please try below code..
public void getData()
 {
     Set<Id> productIds = New Set<Id>();
for(Product2 p : [select id,name,Units_of_Measure__c from product2 where Bundle_Type__c=:selectedname]){
productIds.add(p.Id);
}     
     PriceBook2 pricebook=[select id from pricebook2 where name In('BlackBeltHelp V2')];
    PriceEntry=[SELECT id,Name,Pricebook2Id,Product2Id,ProductCode,UnitPrice,UseStandardPrice FROM PricebookEntry WHERE Pricebook2Id=:pricebook.id and product2IdIN:productIds];
    }

Please let me know if this help!

Thanks
Shivdeep
This was selected as the best answer
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Try this:
product2[] productList=[select id,name,Units_of_Measure__c from product2 where Bundle_Type__c=:selectedname];
PriceBook2[] pricebook=[select id from pricebook2 where name In('BlackBeltHelp V2')];
PriceEntry=[SELECT id,Name,Pricebook2Id,Product2Id,ProductCode,UnitPrice,UseStandardPrice FROM PricebookEntry WHERE Pricebook2Id=:pricebook and product2Id=:productList];
Let me know if it helps.
Thanks!