You need to sign in to do that
Don't have an account?

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
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
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..
In this test class :
You need to insert "new_forecast__c" record first before calling the constructor of your main class. Hopefully it will resolve your problem.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
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.
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..
Create a new user in test class with your required fields as below:
Then create a record as specified by Ankit and finally Run test as the created user like below:
Hope it helps.