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
Ben MorchBen Morch 

System.QueryException: List has no rows for assignment for a Test Class/Method

Hello,

I am trying to deploy some classes that I have updated, but when I deploy them I get an error from a test class that has nothing to do with the classes that I am deploying.  The purpose of the OverrideAccount class is to check to see if the current user should have access to the account or not.  If they should, then give them the standard account page, otherwise show them a VF page with limited data.

Error message from the deployment validation:
System.QueryException: List has no rows for assignment to SObject Stack Trace: Class.OverrideAccount.<init>: line 17, column 1 Class.OverRideAccount_Test.OverRideAccount_test: line 56, column 1

Test Class code:
@isTest
public class OverRideAccount_Test {

    private static testmethod void OverRideAccount_test(){
        user[] u = [select id,name from user where isactive =:true and profile.name!=: 'System Administrator' limit 5];  
  
        // Create account with current logged in user (admin) as owner
        account a = new account() ;
        a.name = 'testName' ;
        a.ownerId = userinfo.getuserid() ;
        system.debug('a ==> '+ A) ;
        insert a ;

        // Create account with general user as owner
        account a1 = new account() ;
        a1.name = 'testName' ;
        a1.ownerId = u[0].id ;
        system.debug('a ==> '+ A1) ;
        insert a1 ;
        
     ...
     ...
    
        //test Override method as the admin
        Apexpages.Standardcontroller std = new ApexPages.StandardController( a) ;
        Apexpages.CurrentPage().getParameters().put( 'id',a.id ) ;
        overRideAccount oa = new overRideAccount(std) ;
        oa.redirect();
        getdomain.DomainURL() ;      
        
        //test Override method as a general user (not the a1 account owner)
        Apexpages.Standardcontroller std1 = new ApexPages.StandardController( a1) ;
        Apexpages.CurrentPage().getParameters().put( 'id',a1.id ) ;
        system.runas(u[2]){
line 56    overRideAccount oa1 = new overRideAccount(std1) ;
                oa1.redirect();
        }
     }
           
}

OverrideAccount Class code:
public account acct {get;set;}
    public account ContRecord {get;set;}
    public profile profile ;  
    public boolean showpage = true;
    Public userRole UserRole ;
    Public userRole loggedInUserRole ;
    Public user user ;
    Public User LoggedinUser ;
    public Id id ;       
    public string domainURL {get;set;}

    public OverrideAccount(apexPages.standardController controller){
          acct = (account)controller.getrecord() ;
          this.id = controller.getid() ;
line 17    ContRecord = [select id,name,ownerId,BillingAddress,ShippingAddress,Billing_Address__c,Shipping_Address__c,BillingStreet ,BillingCity,BillingState,BillingPostalCode,BillingCountry,
                                    ShippingStreet,ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry,CreatedBy.name,lastModifiedBy.name ,CreatedDate,createdby.id,lastmodifiedby.id,
                                    lastmodifieddate,Ultimate_Parent_Owner_ID__c,lastActivityDate,account_availability__c,Account_Link__c 
                                    from account where id = : acct.Id limit 1] ;     
        //domain URL for View hierarchy link on visualforce limited view page
        domainURL =  url.getSalesforceBaseUrl().toExternalForm(); 
        domainURL = getDomain.SfdchostName(); 
        system.debug('Domain URL ==>' + domainURL);
        user = [select id,name,UserRoleId,profileId from user where id =:ContRecord.OwnerId limit 1];
       
        Profile = [select id,name from profile where id = :userinfo.getprofileId() ];                 
        
        if(user!=null){
        UserRole = [select id,parentRoleId,DeveloperName,Name from UserRole where Id=:user.UserRoleId limit 1] ;
            if(UserRole != nuLL){
            LoggedinUser = [select id,name,UserRoleId,profileId from user where id =:userInfo.getUserId() limit 1];  
                if(LoggedinUser.UserRoleId != null){
                loggedInUserRole =  [select id,parentRoleId,DeveloperName,Name from UserRole where Id=:LoggedinUser.userroleId limit 1] ;
                }
            }
        }            
    }
Thanks for any help on this one and let me know if you need further information.

-Ben
 
Best Answer chosen by Ben Morch
Ben MorchBen Morch
It turns out that my issue was corrected by adding "AND UserRole != NULL" to the SOQL query to get the 5 users at the beginning of the Test class.  In the OverrideAccount class we are checking for the UserRole and their were 2 users that did not have a role assigned to them in Production.  

All Answers

Balaji BondarBalaji Bondar
create user in the test class before this line with profile name other than System Administrator.
user[] u = [select id,name from user where isactive =:true and profile.name!=: 'System Administrator' limit 5]; 
 
// Setup test data
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Ben MorchBen Morch
Balaji,

Thank you for your reply.  As you mention we are already doing this with the line that says:
user[] u = [select id,name from user where isactive =:true and profile.name!=: 'System Administrator' limit 5]; 

I am getting 5 users from our current active users that are not a system admin.  In the last 3 lines of the test class I am using the system.runas(u[2]); statement to try to test if the running user gets the override account view or the standard account view.  We are using the ID's of these users to determine their UserRole and match that up with the account owner to determine which account view they should get.

To clarify though, in the error message it mentions line 56 from the test class and line 17 from the OverrideAccount class.  I tried to labeled that in the code snips above with italics and bold print, but obviously it came out as html tags in the code.  
 
Ben MorchBen Morch
It turns out that my issue was corrected by adding "AND UserRole != NULL" to the SOQL query to get the 5 users at the beginning of the Test class.  In the OverrideAccount class we are checking for the UserRole and their were 2 users that did not have a role assigned to them in Production.  
This was selected as the best answer
ao guoao guo
Hello,

I am trying to write a trigger, to implement that the checkbox(primary info) will automatically changed when updated. however I got :
System.QueryException: List has no rows for assignment to SObject Trigger.MyContact_Trigger: line 13, column 1: []: Trigger.MyContact_Trigger: line 29, column 1
User-added image
Thanks for any help on this one and let me know if you need further information.
Ao