You need to sign in to do that
Don't have an account?
Krishna Prasad K P
SOQL query in Apex Trigger throws error (System.QueryException:no rows for assignment) !!
Hi,
I have a 'before insert trigger' on Opportunity. The trigger contains an SOQL select query which returns(may or may not) one record and assigns to an Opportunity variable.
The query statement is:
"Opportunity Opp_variable = [SELECT id,Counter__c,AccountId FROM Opportunity where (AccountId=:New_record.AccountId AND Counter__c!=null) ORDER BY Counter__c DESC LIMIT 1];"
After this statement I have "if(Opp_variable!=NULL)..statements",
But I get the Error message:
"System.QueryException: List has no rows for assignment to SObject"
This is caused because there is no record satisfying my query conditions.. But it should have returned NULL.. ?
Why Am I getting this error..!! SOQL query cannot return null value??
How can I sove this issue..? Please help me
Thanks in Advance..
Krishna
I have a 'before insert trigger' on Opportunity. The trigger contains an SOQL select query which returns(may or may not) one record and assigns to an Opportunity variable.
The query statement is:
"Opportunity Opp_variable = [SELECT id,Counter__c,AccountId FROM Opportunity where (AccountId=:New_record.AccountId AND Counter__c!=null) ORDER BY Counter__c DESC LIMIT 1];"
After this statement I have "if(Opp_variable!=NULL)..statements",
But I get the Error message:
"System.QueryException: List has no rows for assignment to SObject"
This is caused because there is no record satisfying my query conditions.. But it should have returned NULL.. ?
Why Am I getting this error..!! SOQL query cannot return null value??
How can I sove this issue..? Please help me
Thanks in Advance..
Krishna
Instead of using "if(Opp_variable!=NULL)", you can use "if(Opp_variable.size() > 0)".
Try out this, and i think you will be able to get rid of your error.
The reason is, List is something like array, which is not null once initialised, but has the size 0, if there is no elements.
Thanks
V. R.
All Answers
Instead of using "if(Opp_variable!=NULL)", you can use "if(Opp_variable.size() > 0)".
Try out this, and i think you will be able to get rid of your error.
The reason is, List is something like array, which is not null once initialised, but has the size 0, if there is no elements.
Thanks
V. R.
I used array to capture the record, and it works well..!!!!
Thanks a lot Vijay and Simon.. :-)
Regards,
Krishna
Hi Vijay Raut
when i trying to use your way, i again getting an error :
Error: Compile Error: Method does not exist or incorrect signature: [SOBJECT:Account].size() at
code :
Account TempAccount =[select Id from Account LIMIT 1];
if(TempAccount.size()>0)
{
....
}
You have to make an array: