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
rajesh k 10rajesh k 10 

How to solve System.QueryException: List has more than 1 row for assignment to SObject ?

Hi,
         My page:
--------------------

<apex:page controller="ctrollercls sidebar="false">
<apex:sectionHeader title="Account Contact Opportunity Fields"/>
<apex:form >
    <apex:pageBlock >
    <apex:pageBlockButtons location="top">
       <apex:commandButton value="Save" action="{!doSave}"/>
    </apex:pageBlockButtons>
  
    <apex:pageBlockSection title="Contact Fields" rendered="{!iFieldBoolean}">
    <apex:outputField value="{!contactObject.name}"/>
    <apex:outputField value="{!contactObject.Phone}"/>
    <apex:outputField value="{!contactObject.Fax}"/>
        </apex:pageBlockSection>
    <apex:pageBlockSection title="Opportunity Fields" rendered="{!iFieldBoolean}">
    <apex:outputField value="{!opportunityObject.Name}"/>
    <apex:outputField value="{!opportunityObject.CloseDate}"/>
    <apex:outputField value="{!opportunityObject.StageName}"/>
      </apex:pageBlockSection>
       <apex:pageBlockSection title="Account Fields" rendered="{!iFieldBoolean}">
    <apex:outputField value="{!contactObject.Account.name}"/>
        <apex:outputField value="{!contactObject.Account.phone}"/>
   
     </apex:pageBlockSection>
  
    </apex:pageBlock>
</apex:form>
</apex:page>

Controller:
-------------
public class Controllercls{


     public Boolean isBoolean { get; set; }
    
     public List<Opportunity> opportunityObject{get; set;}
   
     public ID cid{get; set;}
           
     public Contact contactObject{get; set;}
    
        public Controllercls()
        {
       
       
        opportunityObject=new List<Opportunity>();
                 if(ApexPages.currentPage().getParameters().get('conid') != NULL)
                 {
                cid = ApexPages.currentPage().getParameters().get('conid');
                }
       
        contactObject=[SELECT id,Name,AccountId,Account.Name,Account.Phone,Fax,Phone FROM Contact WHERE id = :cid];
       
      
         opportunityObject=[Select Id, (Select Id, Contact.Name,CloseDate,StageName From OpportunityContactRoles where  Id =:cid) From Opportunity];
       
        }
        public PageReference doSave() {
               
                try{
                                upsert(contactObject);
                                upsert(opportunityObject);
                       
                        }catch(Exception e)
                        {
                               
                        }
                        pageReference ref=new PageReference('/apex/contactAccountOpportunityFieldsPage?conid='+cid);
                return ref.setRedirect(true);
            }
      
}

help me......
Ruwantha  LankathilakaRuwantha Lankathilaka
You are doing sevaral mistakes as I see.

Even you are parsing a null value you still query the both queries.

It should like this

In controller  

if(ApexPages.currentPage().getParameters().get('conid') != NULL)
                 {
                cid = ApexPages.currentPage().getParameters().get('conid');
contactObject=[SELECT id,Name,AccountId,Account.Name,Account.Phone,Fax,Phone FROM Contact WHERE id = :cid];
       
      
         opportunityObject=[Select Id, (Select Id, Contact.Name,CloseDate,StageName From OpportunityContactRoles where  Id =:cid) From Opportunity];
                }
       else{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Incorrect URL '));

}
        
 Add tag
<apex:pageMessages />

Since you are doing this in the constructor your code is executed on page load.

Identify the exact line that error throws. Copy and paste the error message and let us know the corresponding line.

adityaMorganadityaMorgan
Your controller n vf are totally out of sync... 
opportunityObject=[Select Id, (Select Id, Contact.Name,CloseDate,StageName From OpportunityContactRoles where  Id =:cid) From Opportunity]; 
This query will give u a list of opportutunity and the vf has a code to show single opportunity.

Also Query is wrong. The correct Query will be something :

Select Id, Name,(Select Id,Contact.Name,Opportunity.CloseDate From OpportunityContactRoles where  Contact.Id = '00390000010ubly') From Opportunity where Id = '0069000000LIh1c'

Google about relationship query and this object OpportunityContactRole. you will get some more idea.
Ravikant kediaRavikant kedia
In your controller SOQL query is returning more than 1 record .
Like your query returning more then one opportunity so you have to use list of opportunity instead  of opportunity .