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
Kasia Wojewodzka 7Kasia Wojewodzka 7 

Test Class for Validation rule error System.QueryException: List has no rows for assignment to SObject

Hi  I hope  someone can help me here .  I am new to writting test classes.  

My test class is falinig do to  following error. 
System.QueryException: List has no rows for assignment to SObject  ( line 4, column 1) 

In this test,  User with Role  CAG EMEA Animana CS Rep  should be blocked from  editing field on the  account page: Number of Doctors. 

@istest
private class Test_VR_DontAllowEditforAnimana {
    private static testmethod void updateAccount(){
        User testUser = [Select Id,UserRole.Name,Profile.Name From User 
                        Where UserRole.Name = 'CAG EMEA Animana CS Rep' 
                        AND Profile.Name = 'Field Sales Rep- CAG EMEA'
                        AND Reporting_Role__c = 'FSR DX'
                        AND isActive = true LIMIT 1];

        insert testUser;

        Account testAccount = [SELECT Id FROM Account WHERE Name = 'Test Account' AND SAP_Customer_Number__c ='5678111111' LIMIT 1];
        Insert testAccount;
        Test.startTest();
        system.runAs(testUser){
            
              testAccount = New Account(Number_of_Doctors__c  = 2.00);
            try{
                update testAccount;    
            }catch(Exception e){
                system.debug(e.getMessage());
                system.assertEquals(true,e.getMessage().contains('This modification is not allowed. Please contact your CAG colleague for this field.'));
            }
            System.assertEquals(null,testAccount.id);
            
            
        }
         test.stopTest();
        
    }
}
 


 
Best Answer chosen by Kasia Wojewodzka 7
Debajyoti DhalDebajyoti Dhal
Hi Kasia,

The QueryException is appearing because the SOQL on User object is not returning any value. May be the there is no such record available in the org which is satisfying the WHERE condition. The insert DML after the User & Account query is not required. SOQL provides the value which is available in the org. Use the insert DML whenever you want to insert new record into the org. As part of best practice for test class, always create the required data set in your class. Don't depend upon the org data for test class usage. Please follow this modified version of the code.

@istest
private class Test_VR_DontAllowEditforAnimana {
    private static testmethod void updateAccount(){
        
        //Query the Role & Profile to assign the new User
        UserRole usrRole = [Select Id From UserRole Where UserRole.Name = 'CAG EMEA Animana CS Rep' Limit 1];
        Profile usrProfile = [Select Id from Profile where Profile.Name = 'Field Sales Rep- CAG EMEA' Limit 1];
        
        //Create the User record for test class usage
        User testUser = new User(
            UserRoleId = usrRole.Id,
            ProfileId = usrProfile.Id,
            Username = 'testuser@test.com',
            Alias = 'usrTest',
            Email='testuser@test.com',
            EmailEncodingKey='UTF-8',
            Firstname='Test',
            Lastname='User',
            LanguageLocaleKey='Replace with required value',
            LocaleSidKey='Replace with required value',
            TimeZoneSidKey='Replace with required value',
            Reporting_Role__c = 'FSR DX');
            
            insert testUser;
        
        //Execute the Account DML as created User
        system.runAs(testUser){    
        
        //Create the Account record for test class usage.
        Account testAccount = new Account(Name = 'Test Account', SAP_Customer_Number__c ='5678111111');
        Insert testAccount;
        
        Test.startTest();
            
              testAccount.Number_of_Doctors__c = 2.00;
            try{
                update testAccount;    
            }catch(Exception e){
                system.debug(e.getMessage());
                system.assertEquals(true,e.getMessage().contains('This modification is not allowed. Please contact your CAG colleague for this field.'));
            }
            System.assertEquals(null,testAccount.Number_of_Doctors__c);
        }
         Test.stopTest();
        
    }
}

Follow this link for details on test classes & best practices - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

Thanks,
Debajyoti
 

All Answers

Debajyoti DhalDebajyoti Dhal
Hi Kasia,

The QueryException is appearing because the SOQL on User object is not returning any value. May be the there is no such record available in the org which is satisfying the WHERE condition. The insert DML after the User & Account query is not required. SOQL provides the value which is available in the org. Use the insert DML whenever you want to insert new record into the org. As part of best practice for test class, always create the required data set in your class. Don't depend upon the org data for test class usage. Please follow this modified version of the code.

@istest
private class Test_VR_DontAllowEditforAnimana {
    private static testmethod void updateAccount(){
        
        //Query the Role & Profile to assign the new User
        UserRole usrRole = [Select Id From UserRole Where UserRole.Name = 'CAG EMEA Animana CS Rep' Limit 1];
        Profile usrProfile = [Select Id from Profile where Profile.Name = 'Field Sales Rep- CAG EMEA' Limit 1];
        
        //Create the User record for test class usage
        User testUser = new User(
            UserRoleId = usrRole.Id,
            ProfileId = usrProfile.Id,
            Username = 'testuser@test.com',
            Alias = 'usrTest',
            Email='testuser@test.com',
            EmailEncodingKey='UTF-8',
            Firstname='Test',
            Lastname='User',
            LanguageLocaleKey='Replace with required value',
            LocaleSidKey='Replace with required value',
            TimeZoneSidKey='Replace with required value',
            Reporting_Role__c = 'FSR DX');
            
            insert testUser;
        
        //Execute the Account DML as created User
        system.runAs(testUser){    
        
        //Create the Account record for test class usage.
        Account testAccount = new Account(Name = 'Test Account', SAP_Customer_Number__c ='5678111111');
        Insert testAccount;
        
        Test.startTest();
            
              testAccount.Number_of_Doctors__c = 2.00;
            try{
                update testAccount;    
            }catch(Exception e){
                system.debug(e.getMessage());
                system.assertEquals(true,e.getMessage().contains('This modification is not allowed. Please contact your CAG colleague for this field.'));
            }
            System.assertEquals(null,testAccount.Number_of_Doctors__c);
        }
         Test.stopTest();
        
    }
}

Follow this link for details on test classes & best practices - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

Thanks,
Debajyoti
 
This was selected as the best answer
Kasia Wojewodzka 7Kasia Wojewodzka 7
Thank you for your help Debajyoti!  It worked.  Also, I appriciate the link on test classes and best practices.  A lot of to learn.  Thank you again.