+ Start a Discussion
deryckjderyckj 

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

Error is in expression '{!populateAdvertizingInfo}' in page lead_creation

 

Class.Lead_Creation.populateAdvertizingInfo: line 88, column 1

---------------------------------

 

Error results on VisualForce page that references the following code on my visualforce page.  What's highlighted in orange is what is failing and the referenced code follows below:

 

<apex:selectListvalue="{!SelectedExtension}"size="1"label="Ad Series"id="extension">

                    <apex:selectOptionsvalue="{!ExtensionsOptions}"></apex:selectOptions>

            <apex:actionSupportevent="onchange"action="{!populateAdvertizingInfo}"reRender="leadInterest"/> 

                </apex:selectList>

 

1    private Campaign thisCampaign ;

2   public void populateAdvertizingInfo()

3    {

4        List<String> SelectedAdSeries = SelectedExtension.split('-');

5        Decimal ext = Decimal.valueof(SelectedAdSeries[1]);

6        thisCampaign = [Select Campaign_Fully_Qualified__c, Ad_Type__c, Ad_Size__c, Media_Source__c, Project_Code__c,

7                       Type from Campaign where Project__c =: currentLead.Project_site__c AND Extension__c =: ext];

8        system.debug('the campaign '+thisCampaign.id);

9        currentLead.Ad_Type__c = thisCampaign.Ad_Type__c;

10        currentLead.Ad_Size__c = thisCampaign.Ad_Size__c;

11        currentLead.Media_Source__c = thisCampaign.Media_Source__c;

12        currentLead.Project_Code__c = thisCampaign.Project_Code__c;

13       currentLead.Ad_Series__c = thisCampaign.Campaign_Fully_Qualified__c;

14        SelectedCampaignID = thisCampaign.Id;  

15        SelectedCampaignName = thisCampaign.Campaign_Fully_Qualified__c;

16    }

 

It appears that I am getting hung up on the line 6 above.  Any suggestions?

Best Answer chosen by Admin (Salesforce Developers) 
Tim BarsottiTim Barsotti

Your SOQL query is returning more than one result. You have a few options here: 

 

1) You could cast it to a list instead of a single record.

2) You could add LIMIT 1 to your SOQL query to return only 1 record.

3) You could search by more filtered criteria to only return 1 result

All Answers

Tim BarsottiTim Barsotti

Your SOQL query is returning more than one result. You have a few options here: 

 

1) You could cast it to a list instead of a single record.

2) You could add LIMIT 1 to your SOQL query to return only 1 record.

3) You could search by more filtered criteria to only return 1 result

This was selected as the best answer
deryckjderyckj

Tim, thank you very much.  I know this was an elementary question.  I am new to this environment.  The option of adding a limit of 1 was exactly what I needed!

Tim BarsottiTim Barsotti

No problem. One thing to keep in mind, your query was returning more than one result. Adding the LIMIT 1 does not neccessarily mean your are selecting the correct result. You might want to consider filtering additionally or adding a ORDER BY to ensure you are selecting the correct record.