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 Wild 8Ben Wild 8 

"system.queryexception: list has no rows for assignment to sobject " Page Controller Extension Test Class

I have a controller extension for a custom create child object page to take me back to the correct page after editing and to allow me to have an erorr message wthout using a validation. The controller works exactly as it should but I am having issues writing the test class for it. Iget the error "system.queryexception: list has no rows for assignment to sobject" My code is as below:
Controller:
public with sharing class createController {
	public String oppId {get;set;}
	public Opportunity opp;
	public Opportunity_Forecast__c oppf {get;set;}
	private ApexPages.StandardController stdController; 
	
    public createController(ApexPages.StandardController stdController) {
    	this.stdController = stdController;
        oppf = (Opportunity_Forecast__c)stdController.getRecord();
        String oppid = (oppf.Opportunity__c);
        String ID = ApexPages.currentPage().getParameters().get('oppid');
        opp = [select Automatic_Forecasting__c from Opportunity where Id =:ID]; 
        if (ID != null){
        	oppf.Opportunity__c= ID;   
        }   
    }
	
	public PageReference cancelAndReturn() 
    {
    	PageReference cancel = new PageReference('/' + ApexPages.currentPage().getParameters().get('oppid'));
        cancel.setRedirect(true);
 		return cancel;	
    }
	
	public PageReference saveAndReturn()
    {
        try{
        	if(opp.Automatic_Forecasting__c == true){
        		ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Upside Automation must be disabled on the opportunity page before editing forecasts.');
        		ApexPages.addMessage(myMsg);
        		return null;
        	}
        	else{
        		upsert oppf;
        		PageReference save = new PageReference('/' + ApexPages.currentPage().getParameters().get('oppid'));
        		stdController.save();
        		save.setRedirect(true);
        		return save;
        	}
        }
        catch(DMLException ex){
        	ApexPages.addMessages(ex);
        	return null;
        }
    }   
}
Test Class:
@isTest 
public with sharing class CreateControllerTest {
    static testMethod void testCreateController(){
    	
    	Account testAcct = new Account(Name='Test Create Controller');
    	insert testAcct;
    	
    	Opportunity opp = new Opportunity(Name = 'Test Opportunity1',Account = testAcct,Project_Start_Date__c = Date.newInstance(2017, 12, 15),Delivery_End_Date__c = Date.newInstance(2018, 9, 15),Forecast_Amount__c = 50000, StageName = 'Continued Development of Project',Opportunity_Region__c = 'UK',CloseDate = Date.newInstance(2011, 12, 15), Is_Test__c =TRUE, Automatic_Forecasting__c = FALSE, Test_Date__c = Date.newInstance(2017, 12, 18));
    	insert opp;
    	
    	Opportunity_Forecast__c oppf = new Opportunity_Forecast__c(Opportunity__c = opp.id, Value__c = 2000, Forecast_Category__c = 'Upside', Forecast_Period__c = 'Next Quarter');
    	insert oppf;
    	
    	Test.StartTest();
    	
    	PageReference pageRef = Page.NewForecast;
		Test.setCurrentPage(pageRef);
		pageRef.getParameters().put('id',oppf.Id);
    	ApexPages.StandardController sc = new ApexPages.StandardController(oppf);
        CreateController testOppForecast = new CreateController(sc);
 
        testOppForecast.saveAndReturn();
        Test.StopTest();
    }
}
Any ideas what I'm doing wrong? I've done some reading on the error I'm getting but most cases are where the record hasn't been created. 
Best Answer chosen by Ben Wild 8
Aslam ChaudharyAslam Chaudhary
set oppid in your test class.
ApexPages.currentPage().getParameters().set('oppid',opp.Id);

All Answers

Aslam ChaudharyAslam Chaudhary
set oppid in your test class.
ApexPages.currentPage().getParameters().set('oppid',opp.Id);
This was selected as the best answer
Ben Wild 8Ben Wild 8
HI Aslam, I tried adding that line but it just gives me the "error Method does not exist or incorrect signature: void set(String, Id) from the type Map<String,String>" when I try to save.
Ben Wild 8Ben Wild 8
Ok reliased it just needed to make it .put rather than .set, that lets me save but now its throwing an error saying it can't write the Opportunity__c (lookup) field. "System.SObjectException: Field is not writeable: Opportunity_Forecast__c.Opportunity__c". I've had a look and the field isnt set to read only. Any ideas?

 
v varaprasadv varaprasad
Hi Ben,

Please check following code once: 
I have added Accountid = testAcct.id, line only.

 
@isTest 
public with sharing class CreateControllerTest {
    static testMethod void testCreateController(){
    	
    	Account testAcct = new Account(Name='Test Create Controller');
    	insert testAcct;
    	
    	Opportunity opp = new Opportunity(Name = 'Test Opportunity1',Accountid = testAcct.id,Project_Start_Date__c = Date.newInstance(2017, 12, 15),Delivery_End_Date__c = Date.newInstance(2018, 9, 15),Forecast_Amount__c = 50000, StageName = 'Continued Development of Project',Opportunity_Region__c = 'UK',CloseDate = Date.newInstance(2011, 12, 15), Is_Test__c =TRUE, Automatic_Forecasting__c = FALSE, Test_Date__c = Date.newInstance(2017, 12, 18));
    	insert opp;
    	
    	Opportunity_Forecast__c oppf = new Opportunity_Forecast__c(Opportunity__c = opp.id, Value__c = 2000, Forecast_Category__c = 'Upside', Forecast_Period__c = 'Next Quarter');
    	insert oppf;
    	
    	Test.StartTest();
    	
    	PageReference pageRef = Page.NewForecast;
		Test.setCurrentPage(pageRef);
		pageRef.getParameters().put('id',oppf.Id);
    	ApexPages.StandardController sc = new ApexPages.StandardController(oppf);
        CreateController testOppForecast = new CreateController(sc);
 
        testOppForecast.saveAndReturn();
        Test.StopTest();
    }
}


Thanks
Varaprasad
 
Ben Wild 8Ben Wild 8
Hi Varaprasad, I gave that a try but it gave me the original error.
Ben Wild 8Ben Wild 8
Ok I've figured it out, I had to allow the object to be reparentable (no massive issue as the user cannot see the relationship field on the edit page anyway so wouldnt be able to do this by accident). I'm guessing having that setting on makes the field read only even if access is set to read/write.