1

when I tried executing this code I get the error on the visualforce page

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

visualforce page:

<apex:page id="RelatedContactsInlineVF"
           standardController="Contact"
           extensions="RelatedContactsInlineContExt"
           title="Related Accounts" showChat="false" showHeader="false" sideBar="false">

    <apex:form >
    <apex:pageBlock >
       <apex:pageBlockTable value="{!conRec}" var="cont">
          <apex:column value="{!cont.Name}"/>
       </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

controller class

public without sharing class RelatedContactsInlineContExt {
    public List<Contact> conRec{get;set;}
    public id conRecId;



    public RelatedContactsInlineContExt(ApexPages.StandardController stdController) {


    if(ApexPages.currentPage().getParameters().get('id')!=null){

    conRecId = [select id from contact where MailingCity=
    :ApexPages.currentPage().getParameters().get('MailingCity')].id;

    if(conRecId!= null){
        conRec=[select id, Name, MailingCity, Phone from contact where id =: conRecId];

    }
    }                       
    }
}
2
  • 3
    Problem is that there are multiple contacts with MailingCity equal to ApexPages.currentPage().getParameters().get('MailingCity'), so that's why it fails on assignment of conRecId
    – kurunve
    Commented Feb 3, 2017 at 8:23
  • Do you need to display single record or multiple
    – Reshma
    Commented Feb 3, 2017 at 8:52

3 Answers 3

2

You can avoid this exception by replacing this line.

conRecId = [select id from contact where MailingCity =: ApexPages.currentPage().getParameters().get('MailingCity')].id;

to

conRecId = [SELECT id 
           FROM contact 
           WHERE MailingCity =: ApexPages.currentPage().getParameters().get('MailingCity') 
           LIMIT 1].id; //Put limit 1 if you need to display only one record.

Or

//As if you pass id along with url query using that id instead of mailing city
conRecId = [SELECT id 
            FROM contact 
            WHERE Id =: ApexPages.currentPage().getParameters().get('Id')].id;

If you want to display multiple contact record try this code. You don't need a conRecId to store an id value.

public without sharing class RelatedContactsInlineContExt {
    public List<Contact> conRec{get;set;}
    public RelatedContactsInlineContExt(ApexPages.StandardController stdController) {
        conRec = new List<Contact>();
        //Query all contacts matches with mailingcity parameter
        conRec = [SELECT id, Name, MailingCity, Phone 
                  FROM contact 
                  WHERE MailingCity =: ApexPages.currentPage().getParameters().get('MailingCity')];
    }
}
1
  • I found that ApexPages.currentPage().getParameters() gives us only ID value and more over implementing the above code gives me the entire set of contacts.
    – vishnusait
    Commented Feb 3, 2017 at 17:20
0

You have more than one Contact record where Mailing City is same as passed parameter to page. So Below code will result in exception as it is expecting only single record based on Query.

conRecId = [select id from contact where MailingCity=
    :ApexPages.currentPage().getParameters().get('MailingCity')].id;
0
 public without sharing class RelatedContactsInlineContExt {
 public contact myContact{get;set;}
 public List<contact> SelectedContacts{get;set;}


  public RelatedContactsInlineContExt(ApexPages.StandardController stdController) {
    String query = 'SELECT Name, MailingCity, Phone FROM Contact WHERE Id = \'' + stdController.getId() + '\' LIMIT 1';
    myContact = Database.query(query);      

    //myContact =     SELECT Name, MailingCity, Phone FROM Contact WHERE Id = :stdController.getId();


    SelectedContacts = [select Id, Name, MailingCity, Phone from Contact where MailingCity = :myContact.MailingCity ];
 }
 }

this helped me in my case

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .