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
Prasanth RPrasanth R 

error in Vf

i'm trying to generate the invoice using visual force but getting this error System.QueryException: List has more than 1 row for assignment to SObject..kindly someone assist me.below i mention the code

apex class:

global with sharing class CustomerController {
    
    public Customer__c cust {get;set; }
    
    public CustomerController(){
        cust = [Select Name,
                Delivery_Address__c,
                Phone__c,
                Product_cost__c,
                Product_Name__c,
                Delivery_cost__c,
                Total_cost__c
                from Customer__c];
    }
}


VF page:
<apex:page Controller="CustomerController">
        <apex:pageBlock >
          <apex:pageBlockSection title="Invoice">
             <apex:outputText value="{!cust.Name}"/><br/>
             <apex:outputText value="{!cust.Delivery_Address__c}"/><br/>
             <apex:outputText value="{!cust.Phone__c}"/><br/>
             <apex:outputText value="{!cust.Product_cost__c}"/><br/>
             <apex:outputText value="{!cust.Product_Name__c}"/><br/>
             <apex:outputText value="{!cust.Delivery_cost__c}"/><br/> 
              <apex:outputText value="{!cust.Total_cost__c}"/><br/> 
           </apex:pageBlockSection>
        </apex:pageBlock>
</apex:page>

User-added image
Best Answer chosen by Prasanth R
ANUTEJANUTEJ (Salesforce Developers) 
Hi Prasanth,

The reason for the error is that you are fetching more than one records in the soql and assigning it to a single record variable so to rectify this you can use either limit keyword in soql or convert the variable to a list and iterate in vfpage.

1st option

Try changing the above code to below:
 
global with sharing class CustomerController {
    
    public Customer__c cust {get;set; }
    
    public CustomerController(){
        cust = [Select Name,
                Delivery_Address__c,
                Phone__c,
                Product_cost__c,
                Product_Name__c,
                Delivery_cost__c,
                Total_cost__c
                from Customer__c LIMIT 1];
    }
}


VF page:
<apex:page Controller="CustomerController">
        <apex:pageBlock >
          <apex:pageBlockSection title="Invoice">
             <apex:outputText value="{!cust.Name}"/><br/>
             <apex:outputText value="{!cust.Delivery_Address__c}"/><br/>
             <apex:outputText value="{!cust.Phone__c}"/><br/>
             <apex:outputText value="{!cust.Product_cost__c}"/><br/>
             <apex:outputText value="{!cust.Product_Name__c}"/><br/>
             <apex:outputText value="{!cust.Delivery_cost__c}"/><br/> 
              <apex:outputText value="{!cust.Total_cost__c}"/><br/> 
           </apex:pageBlockSection>
        </apex:pageBlock>
</apex:page>

2nd option is to change public Customer__c cust {get;set; } to public list<Customer__c> cust {get;set; } and iterate over the list in vfpage.

To learn on how to show list of records you can check this link: 
>> https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_repeat.htm
>>https://developer.salesforce.com/forums/?id=906F0000000BX9mIAG

To learn more about the above error, you can check this link:  https://salesforce.stackexchange.com/questions/34174/system-queryexception-list-has-more-than-1-row-for-assignment-to-sobject/34175

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Prasanth,

The reason for the error is that you are fetching more than one records in the soql and assigning it to a single record variable so to rectify this you can use either limit keyword in soql or convert the variable to a list and iterate in vfpage.

1st option

Try changing the above code to below:
 
global with sharing class CustomerController {
    
    public Customer__c cust {get;set; }
    
    public CustomerController(){
        cust = [Select Name,
                Delivery_Address__c,
                Phone__c,
                Product_cost__c,
                Product_Name__c,
                Delivery_cost__c,
                Total_cost__c
                from Customer__c LIMIT 1];
    }
}


VF page:
<apex:page Controller="CustomerController">
        <apex:pageBlock >
          <apex:pageBlockSection title="Invoice">
             <apex:outputText value="{!cust.Name}"/><br/>
             <apex:outputText value="{!cust.Delivery_Address__c}"/><br/>
             <apex:outputText value="{!cust.Phone__c}"/><br/>
             <apex:outputText value="{!cust.Product_cost__c}"/><br/>
             <apex:outputText value="{!cust.Product_Name__c}"/><br/>
             <apex:outputText value="{!cust.Delivery_cost__c}"/><br/> 
              <apex:outputText value="{!cust.Total_cost__c}"/><br/> 
           </apex:pageBlockSection>
        </apex:pageBlock>
</apex:page>

2nd option is to change public Customer__c cust {get;set; } to public list<Customer__c> cust {get;set; } and iterate over the list in vfpage.

To learn on how to show list of records you can check this link: 
>> https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_repeat.htm
>>https://developer.salesforce.com/forums/?id=906F0000000BX9mIAG

To learn more about the above error, you can check this link:  https://salesforce.stackexchange.com/questions/34174/system-queryexception-list-has-more-than-1-row-for-assignment-to-sobject/34175

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Prasanth RPrasanth R
i tried 1st method it's working..