function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SK R.ax1448SK R.ax1448 

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

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!

 

bob_buzzardbob_buzzard

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.

 

 

 

 

SK R.ax1448SK R.ax1448

Thanks for the reply.

Raj.ax1558Raj.ax1558

Befor Select query write the condition

 

if(accIds.size() > 0)

{

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

}

 

 

Thank you

SK R.ax1448SK R.ax1448

Thank you for the response.

 

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