+ Start a Discussion
jOseMCjOseMC 

System.QueryException: List has no rows for assignment to SObject, someone help me

Hey guys, I need some help, in my code line the following error appears: "System.QueryException: List has no rows for assignment to SObject." the line that the error refers to
ServiceContract scQuery = [SELECT BankId__c, Agencia__c, Conta__c, CNPJ__c, Digito_Conta__c,Type_de_Conta_Bancaria__c, Conta_Savings__c from ServiceContract limit 1 ];

how do i fix it?
Best Answer chosen by jOseMC
ravi soniravi soni
HI jOseMC,
try like below.
list<ServiceContract > scQuery = [SELECT BankId__c, Agencia__c, Conta__c, CNPJ__c, Digito_Conta__c,Type_de_Conta_Bancaria__c, Conta_Savings__c from ServiceContract limit 1 ];
you are getting error because you are not getting any data. in this case always store data in list even you are getting only one record.
it is best practice.

don't forget to mark it as best answer.
​​​​​​​Thank you
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

Error says, you dont have any records in servicecontract object.

Can you run the same query in developer console and check, you have records or not.

If this helps, Please mark it as best answer.

Thanks!!
 
ravi soniravi soni
HI jOseMC,
try like below.
list<ServiceContract > scQuery = [SELECT BankId__c, Agencia__c, Conta__c, CNPJ__c, Digito_Conta__c,Type_de_Conta_Bancaria__c, Conta_Savings__c from ServiceContract limit 1 ];
you are getting error because you are not getting any data. in this case always store data in list even you are getting only one record.
it is best practice.

don't forget to mark it as best answer.
​​​​​​​Thank you
 
This was selected as the best answer