Reply
Regular Contributor
SK R
Posts: 44
0

System.QueryException: List has no rows for assignment to SObject.

[ Edited ]

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!

 

Moderator
bob_buzzard
Posts: 6,426
0

Re: System.QueryException: List has no rows for assignment to SObject.

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.

Regular Contributor
SK R
Posts: 44
0

Re: System.QueryException: List has no rows for assignment to SObject.

Thanks for the reply.

Regular Contributor
Raj
Posts: 41
0

Re: System.QueryException: List has no rows for assignment to SObject.

Befor Select query write the condition

 

if(accIds.size() > 0)

{

accList = [ select CaseNum__c from Account where Id in: accIds];

}

 

 

Thank you

Regular Contributor
SK R
Posts: 44
0

Re: System.QueryException: List has no rows for assignment to SObject.

Thank you for the response.

 

I'm getting error in the test class only. Trigger is working as expected.