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
1111_forcecom1111_forcecom 

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

Hi developers,

I have my apex class deploy in my production instance and before I test this class and this was OK. When I try the implement my visualforce page I have this error "System.QueryException: List has more than 1 row for assignment to SObject" I prove this in my test instance and work fine. Please help me why is the problem.

This is my extension class

public class contactExtension {
    public contactExtension(ApexPages.StandardController contactController) {
       this.contact = (Contact)contactController.getRecord();
       idContact = contact.id;
    }
        
    public List<Beneficio__c> getBeneficiosBob()
    {
        Moneda__c monedabob = [select Name from Moneda__c where Moneda__c.name = 'Bolivianos'];
        idMonedabob = monedabob.id;
        x = 0;
        for (Beneficio__c ben : [select monto_estimado__c from Beneficio__c where Contacto__c = :idContact and Moneda__c = :idMonedabob]){
            if (ben.monto_estimado__c == null){
                ben.monto_estimado__c = 0;
            }
            x = ben.monto_estimado__c + x;
        }
              
        return [select Catalogo_Beneficio__c, Name, Moneda__c, monto_estimado__c, proposito__c,fecha_entrega__c from Beneficio__c where Contacto__c = :idContact and Moneda__c = :idMonedabob];
    }          
}

This is my visualforce page

<apex:page StandardController="Contact" extensions="contactExtension" >
  <apex:pageBlock title="Beneficios Bolivianos">
    <apex:pageBlockTable value="{!BeneficiosBob}" var="beneficio" cellPadding="3" border="1" columnsWidth="50px,110px,60px,60px,230px,30px" rules="all">
        <apex:column value="{!beneficio.Name}"/>
        <apex:column value="{!beneficio.Catalogo_Beneficio__c}"/>
        <apex:column value="{!beneficio.Moneda__c}"/>
        <apex:column value="{!beneficio.monto_estimado__c}" style="text-align:right;"/>
        <apex:column value="{!beneficio.proposito__c}"/>
        <apex:column value="{!beneficio.fecha_entrega__c}"/>
    </apex:pageBlockTable>
      <div class="resaltado">
         Total Bolivianos:       
         <apex:outputText value="{0,number,#,##0.00}" style="padding-left: 10px;">   
           <apex:param value="{!x}"/>
         </apex:outputText>
      </div>
  </apex:pageBlock>
</apex:page>

Regards,
Best Answer chosen by 1111_forcecom
Deepak RamaDeepak Rama

public List<Beneficio__c> getBeneficiosBob()
    {
        Moneda__c monedabob = [select Name from Moneda__c where Moneda__c.name = 'Bolivianos'];
        idMonedabob = monedabob.id;
...
}
The SOQL query above is tied to data from a particular org. In the test org, you might be having only 1 record in Moneda__c object by name "Bolivianos". Where as in prod you might be having more than one record. Resolution is to change this to list as below:

public List<Beneficio__c> getBeneficiosBob()
    {
        List<Moneda__c> monedabob = [select Name from Moneda__c where Moneda__c.name = 'Bolivianos'];
    if (monedabob.size() > 0)
{
        idMonedabob = monedabob[0].id;
}
...
}


All Answers

Deepak RamaDeepak Rama

public List<Beneficio__c> getBeneficiosBob()
    {
        Moneda__c monedabob = [select Name from Moneda__c where Moneda__c.name = 'Bolivianos'];
        idMonedabob = monedabob.id;
...
}
The SOQL query above is tied to data from a particular org. In the test org, you might be having only 1 record in Moneda__c object by name "Bolivianos". Where as in prod you might be having more than one record. Resolution is to change this to list as below:

public List<Beneficio__c> getBeneficiosBob()
    {
        List<Moneda__c> monedabob = [select Name from Moneda__c where Moneda__c.name = 'Bolivianos'];
    if (monedabob.size() > 0)
{
        idMonedabob = monedabob[0].id;
}
...
}


This was selected as the best answer
1111_forcecom1111_forcecom
Thank you Deepak