You need to sign in to do that
Don't have an account?
Andy Stollmeyer
VF Test Class Not Finding Account Object
I've created a VF page that works great. However, I cannot get the test class to pass for the life of me. Below is an exceprt from my VF extension:
When I run the test I get the error "System.QueryException: List has no rows for assignment to SObject" pointing to line 15 for the extension class and line 45 for the test class.
The confusing part is the Account is defined, as it saves the Account fields when I save the page. Additionally, when I debug there is an Account being selected, so no idea how the list is ending up empty...
public class OpportunityWonExt{ ApexPages.StandardController controller; public Opportunity theOpp {get;set;} public Account act {get; set;} public Contract ct {get; set;} public OpportunityWonExt(ApexPages.standardController controller){ theOpp = (Opportunity)controller.getRecord(); theOpp.CloseDate = Date.today(); theOpp.StageName = 'Closed Won'; String actid = theOpp.AccountID; OpportunityWonProcess__c settings = OpportunityWonProcess__c.getInstance('Standard Settings'); String ctOwner = settings.Contract_Owner__c; act = [SELECT Id, Name, Company_Size_Type__c FROM Account WHERE ID =:actid ]; system.debug('LOOK HERE act' + act); ct = new Contract(); ct.AccountID = theOpp.AccountID; ct.Status = 'Draft'; ct.ContractTerm = 12; ct.OwnerID = ctOwner;Below is an excerpt from my test class
@isTest(SeeAllData=true) private Class OpportunityWonTest{ static testMethod void theTests(){ // setup some variables String standardPriceBookId = ''; String UID = ''; String opportunityName = 'This Is My Favorite Opportunity1234'; User u = [SELECT ID FROM User WHERE IsActive = TRUE LIMIT 1]; UID = u.ID; // Select pricebook PriceBook2 pb2Standard = [SELECT ID FROM Pricebook2 WHERE IsStandard = TRUE]; standardPriceBookId = pb2Standard.Id; // Setup Account Account a = new Account(Name = 'Test Account',Website = 'www.test.com',Company_Size_Type__c = 'Enterprise',OwnerID=UID); insert a; // Setup Opportunity Date closedate = Date.today(); Opportunity o = new Opportunity(Account = a, Name=opportunityName, StageName='Prospecting', CloseDate=Date.today(),Logo_Use__c = 'Yes', OwnerID=UID); insert o; Opportunity opp = [Select ID from Opportunity WHERE Name = 'This Is My Favorite Opportunity1234']; // set up product2 Product2 p2 = new Product2(Name='Test Product',isActive=true); insert p2; // set up PricebookEntry PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true); insert pbe; // set up OpportunityLineItem OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id, Quantity=1, TotalPrice=99, Discount__c=0, Contract_Length__c = 12); insert oli; // load the page PageReference pageRef = Page.OpportunityWon; pageRef.getParameters().put('Id',opp.id); Test.setCurrentPageReference(pageRef); // load the extension ApexPages.StandardController newcontroller = new ApexPages.StandardController(opp); OpportunityWonExt OppWonExt = new OpportunityWonExt(newcontroller);
When I run the test I get the error "System.QueryException: List has no rows for assignment to SObject" pointing to line 15 for the extension class and line 45 for the test class.
The confusing part is the Account is defined, as it saves the Account fields when I save the page. Additionally, when I debug there is an Account being selected, so no idea how the list is ending up empty...
Hi Andy,
Try breaking out the AccountId field initialization from line 24 in your test class to the line below like so:
Let me know if that works.
Best,
Scott
All Answers
The SOQL query on line 24 of your test class only pulls in the Id value of the test opportunity record but on line 12 of the controller you try to use its AccountId field. This field will be null since the opportunity SOQL query did not search for it meaning on line 15 you get no result from the Account SOQL query. Try changing line 24 of your test class to the following:
Hope that helps,
Scott
Hi Andy,
Try breaking out the AccountId field initialization from line 24 in your test class to the line below like so:
Let me know if that works.
Best,
Scott
Thanks for the help!