+ Start a Discussion
Felipe SenlerFelipe Senler 

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

Greetings, Ive just started a few days ago with Apex by work as a junior developer at the moment, heres my issue:

This trigger needs to transfer Integer values from a custom Object called Encuesta__c(Survey__c) with its a custom object, to three custom fields (Num_Prom_Cobr__c,Num_Prom_Perp__c,Num_Prom_Sin__c) from Account after being fired.

Heres the trigger code called traspasoValoresListas (transferListValues):

trigger traspasoValoresListas on Encuesta__c (before insert , before update) {

Encuesta__c[] encu = Trigger.new;

Integer sin; /*These three Integers are to contain the upcoming values of APEX class called
Integer cob; ConvertirStringValores with functions that converts the values of a Seleccion list into Integers*/
Integer per;

for (Integer i=0;i<encu.size();i++)
{
     sin=ConvertirStringValores.convertirListasSiniestros(encu[i]);//Turns the value Selectionlist called Siniestros(Fires)(String) to Integer
     per=ConvertirStringValores.convertirListasPercepcion(encu[i]);//Turns the value Selectionlist called Percepcion(Perception)(String) to Integer
     cob=ConvertirStringValores.convertirListasCobranza(encu[i]);//Turns the value Selectionlist called Cobranza(Collections)(String) to Integer
    //All of these values coming from the Custom Object Encuesta__c assigned as encu;
}
//The code sadly goes downhill from here
Account a = [SELECT Num_Prom_Cobr__c,Num_Prom_Perp__c,Num_Prom_Sin__c
                   FROM Account];

a.Num_Prom_Cobr__c=cob;
a.Num_Prom_Perp__c=per;
a.Num_Prom_Sin__c=sin;

update a;
}

Any help is highly appreciated!
Felipe.
Best Answer chosen by Felipe Senler
RoyalRoyal
HI,
in salesforce SOQL query alway returns List<SObjects>, so can change you are quey like this
list<Account> a = [SELECT Num_Prom_Cobr__c,Num_Prom_Perp__c,Num_Prom_Sin__c
                   FROM Account];
List<Account> acc=new List<Account>();
for(Account aa : a){
aa.Num_Prom_Cobr__c=cob;
aa.Num_Prom_Perp__c=per;
aa.Num_Prom_Sin__c=sin;

acc.add(aa);
}
update acc;

i hope this can help you

All Answers

RoyalRoyal
HI,
in salesforce SOQL query alway returns List<SObjects>, so can change you are quey like this
list<Account> a = [SELECT Num_Prom_Cobr__c,Num_Prom_Perp__c,Num_Prom_Sin__c
                   FROM Account];
List<Account> acc=new List<Account>();
for(Account aa : a){
aa.Num_Prom_Cobr__c=cob;
aa.Num_Prom_Perp__c=per;
aa.Num_Prom_Sin__c=sin;

acc.add(aa);
}
update acc;

i hope this can help you
This was selected as the best answer
Felipe SenlerFelipe Senler
@Royal, Works like a charm!
One more thing
for(Account aa : a) -----> could you kindly explain the use of colon for that for loop?, didn't quite got that

But again, you made a babystep programmer Happy!, Thank you!
RoyalRoyal
sure,
a contain list of account records wich is return by query, just assume a have 10 records so 10 records we can seperate one by one record by using for loop, like each time one records will add to aa the for loop run, next time second records will store in aa then continue for loop like that untill complete the 10 records loop will run later loop will exit,  that is the logic in for loop.

because in list we can not access directly perticular records with out for loop.


i hope this will help you.