Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- System.QueryException: List has no rows for assign...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
System.Que ryExceptio n: List has no rows for assignment to SObject.
[ Edited ]
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 02:54 AM - edited 12-14-2012 03:00 AM
Hi All,
I'm trying to write a unit test for a trigger test and getting the following error message:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InsertCaseTrigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject.
When I use @isTest(SeeAllData=true), i dont see any error messages, but code is not covered 75%. Why this is heppening ?
Can some one help me please.
Trigger:
trigger updateCaseSummary on Case(after insert) {
List<Account> accList = new List<Account>();
set<Id> accIds = new set<Id>();
for(Case cs : Trigger.new){
accIds.add(cs.AccountId);
}
accList = [ select CaseNum__c from Account where Id in : accIds];
if(accList.size() > 0) {
for(Account ac: accList){
if( ac.CaseNum__c == null)
ac.CaseNum__c = 0;
ac.CaseNum__c = ac.CaseNum__c + 1;
}
update accList;
}
Test Class:
@isTest
private class updateCaseSummaryTest{
static testMethod void SummaryTest(){
Account acc = New Account(Name = 'TestAccount',CaseNum__c = 1);
insert acc;
Case cs = New Case(AccountId = acc.Id);
insert cs;
Account acc2 = New Account(Name = 'TestAccount2',CaseNum__c =null);
insert acc2;
Case cs2 = New Case(AccountId = acc2.Id);
insert cs2;
}
}
Thanks a ton in advance!
Re: System.Que ryExceptio n: List has no rows for assignment to SObject.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 03:05 AM
This looks like an error being thrown from a different trigger - the error mentions BeforeInsert, which could be on account or case.
This error usually means that the code is expecting exactly one item, something like:
Account acc=[select id, Name from Account where name='Test'];
But because the real data can't be seen, there are no matching rows returned.
Certified Salesforce Technical Architect, Developer, Advanced Developer, Administrator, Advanced Administrator, Consultant, Sales Cloud Consultant,Service Cloud Consultant
Force.com MVP | The Bob Buzzard Blog | Linked In | Twitter
I don't respond to private messages/emails asking for help.
Take the Bob Buzzard Sobject Fields quiz.
Re: System.Que ryExceptio n: List has no rows for assignment to SObject.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 03:24 AM
Thanks for the reply.
Re: System.Que ryExceptio n: List has no rows for assignment to SObject.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 04:51 AM
Befor Select query write the condition
if(accIds.size() > 0)
{
accList = [ select CaseNum__c from Account where Id in: accIds];
}
Thank you
Re: System.Que ryExceptio n: List has no rows for assignment to SObject.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 06:14 AM
Thank you for the response.
I'm getting error in the test class only. Trigger is working as expected.

