+ Start a Discussion
MigMig 

APEX : List has no rows for assignement to SObject problem


My trigger works fine when it returns data. But sometimes The query return anything and I've got this problem.
BonusTrigger: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject:

Code:
Bonus_definition__c[] bonusdef = new Bonus_definition__c[1];
/* definition of all the vars

Bonus_definition__c s = [Select b.id, b.Convertion_Rate__c, b.BonusCurrency__c From Bonus_definition__c b where b.Year__c =:bonusyear and  b.BonusCurrency__c =:curr limit 1];

 

When this query returns anything I've got a problem. How to solve it ?
Thank you in advance for your help

           



Message Edited by Mig on 03-18-2008 05:32 PM
Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
The assignment to a single object requires the query to return a single row, if the query may not return a row, you should assign it to an array, then examine the size of the array to determine if you got a row or not, and take the appropriate action.

All Answers

SuperfellSuperfell
The assignment to a single object requires the query to return a single row, if the query may not return a row, you should assign it to an array, then examine the size of the array to determine if you got a row or not, and take the appropriate action.
This was selected as the best answer
MigMig
Yep It works. Thank you very much for your quick answer.

Code:
Bonus_definition__c[] bonusdef = [select ...] ;

if(bonusdef.size() > 0){
//...
}else{
//...
{