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

"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:
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.
ApexPages.currentPage().getParameters().set('oppid',opp.Id);
All Answers
ApexPages.currentPage().getParameters().set('oppid',opp.Id);
Please check following code once:
I have added Accountid = testAcct.id, line only.
Thanks
Varaprasad