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
vishwa attikerivishwa attikeri 

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

Hi,

Any one please solve this issue 

 

when i am going to run the test class it will show an error System.QueryException: List has no rows for assignment to SObject 

and Class.SalesrepForecastUpdatetest.testsearch: line 66, column 9 External entry point

 

This is my  Apex class code sample  

 

 

User usr = [Select id,Alias,lastname,firstname,ContactId,Profile.name from User where id=:UserInfo.getUserId() Limit 1];

    public List<CustForecastWrapper> getCustForecasts(){ 

      if(custForecasts != null){

            return custForecasts;        } 

      custForecasts = new List<CustForecastWrapper>();

        if(usr.id == null){

            return custForecasts;   

    } 

      New_Forecast__c CustFCst=new New_Forecast__c(); 

              CustFCst=[select id,name from new_forecast__c where Region__r.sales_Rep__c=:usr.id limit 1];

           '''''''''''''''''''''

''''''''''''''''''

'''''''''''''

}

 

    public void SearchForecast(){    

   custForecasts = null;

   prodForecasts = null;             

  if(ObjectType == 'Product'){            

  ForeCastTitle = Year + ' Product Forecast';            

    getProdForecasts();   

    }        

      if(ObjectType=='Region'){                  

    if(usr.firstname == null)         

      ForeCastTitle = '';   

        else        

        ForeCastTitle = Year + ' Region Forecast for ' + usr.firstname;                  

  getCustForecasts();   

    }

        }

 

Test Method:

 

    static testMethod void testsearch(){ 

    User usr = [Select id from User where firstname=:'test' Limit 1];     

     SalesrepForecastUpdate con=new  SalesrepForecastUpdate (); 

      con.ObjectType = 'Region';    con.Year = String.ValueOf(Date.today().year()); 

         con.searchforecast();              

 List<CustForecastWrapper> forecasts = con.getCustForecasts();                                        }

 

 

 

Thanks 

srikeerthisrikeerthi

Hi

 

Please check the condition you are specifying in the Query that the records are existing or not.

what is the 66 line in your controller.

In your test class the user 'test' is he exists?

 

 

Thanks

 

vishwa attikerivishwa attikeri

Hi,

 

Thanks for your Responce

 

in line 66 i have 

  CustFCst=[select id,name from new_forecast__c where Region__r.sales_Rep__c=:usr.id limit 1];

 

and 'test' user is not exist but when i give the existing user first name then also the same think happen

 

 

Thanks..

Ankit AroraAnkit Arora

In this test class :

 

static testMethod void testsearch(){ 
    User usr = [Select id from User where firstname=:'test' Limit 1];     
     SalesrepForecastUpdate con=new  SalesrepForecastUpdate (); 
      con.ObjectType = 'Region';    con.Year = String.ValueOf(Date.today().year()); 
         con.searchforecast();              
 List<CustForecastWrapper> forecasts = con.getCustForecasts();                                        }

 

You need to insert "new_forecast__c" record first before calling the constructor of your main class. Hopefully it will resolve your problem.

 

static testMethod void testsearch(){ 
    User usr = [Select id from User where firstname=:'test' Limit 1];
     
    //Fill all mandatory fields here
    new_forecast__c tempObj = new new_forecast__c(name='Test') ;
    insert tempObj ;

    SalesrepForecastUpdate con=new  SalesrepForecastUpdate (); 

      con.ObjectType = 'Region';    con.Year = String.ValueOf(Date.today().year()); 
         con.searchforecast();              
 List<CustForecastWrapper> forecasts = con.getCustForecasts();                                        }

 

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

why are you doing this

User usr = [Select id from User where firstname=:'test' Limit 1];     

I think think this si what causing the issue , do you have any user with first name Test, if not surly this is giving you error. What do you want to achieve with this, is there any reason for doing this let me know.

vishwa attikerivishwa attikeri

Hi,

 

Created Different user for sales rep Profile and i have Created Region__c custom object in this Sales Rep field is look up to user, When the different user will login it will shows only related region sales rep(user) data 

 

for this im using Query as 

User usr = [Select id,Alias,lastname,firstname,ContactId,Profile.name from User where id=:UserInfo.getUserId() Limit 1];

 

 New_Forecast__c CustFCst=new New_Forecast__c(); 

              CustFCst=[select id,name from new_forecast__c where Region__r.sales_Rep__c=:usr.id limit 1];

 

 

 

Thanks..

 

 

Rahul SharmaRahul Sharma

Create a new user in test class with your required fields as below:

User objUser1 = new User(
					ProfileId = [select id from Profile where Name='System Administrator' limit 1].Id, 
					LanguageLocaleKey = 'en_US', 
					LocaleSidKey = 'en_US', 
					TimeZoneSidKey = 'America/Chicago', 
					EmailEncodingKey = 'UTF-8', 
					LastName = 'Test User1',
					Alias = 'TestU1', 
					Email = 'Testuser@gmail.com', 
					Username = 'Testuser@gmail.com', 
					CommunityNickname = 'TestUser 12345' 
				);
insert objUser1;

 Then create a record as specified by Ankit and finally Run test as the created user like below:

 

system.runAs(objUser1)
{
     // your Code
}

 Hope it helps.