You need to sign in to do that
Don't have an account?
CloudNerd
Error in Apex Test Class
I am trying to write a test class that inserts a new Opportunity and then a new custom object "Project" and then relates the opportunity to the Project.
I keep getting the following error in the code below when running the test:
"System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: MPM4_BASE__Milestone1_Project__c.Opportunity__r"
I keep getting the following error in the code below when running the test:
"System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: MPM4_BASE__Milestone1_Project__c.Opportunity__r"
@isTest private class newMilestoneTestClass { static testMethod void validatenewMilestone() { Opportunity o = new Opportunity(Name='Test Opp', CloseDate=date.today(), StageName='Closed Won'); // Insert opportunity insert o; MPM4_BASE__Milestone1_Project__c p = new MPM4_BASE__Milestone1_Project__c(Name='Test Project', MPM4_BASE__Status__c='Set', Opportunity__c=o.Id); System.debug('Status before inserting new project: ' + p.MPM4_BASE__Status__c); System.debug('Opp is: ' + p.Opportunity__c); // Insert project insert p; // Retrieve the new project p = [SELECT MPM4_BASE__Status__c FROM MPM4_BASE__Milestone1_Project__c WHERE Id = :p.Id]; // Test that the trigger correctly updated the related Opportunity System.assertEquals(o.Id, p.Opportunity__r.Id); } }
The API name of Opportunity field on Project is Opportunity__c so you have to query this field and use the same in assert statement.
Please update your test class as below :
Let me know if you have any issue.
Thanks,
Abhishek
All Answers
The API name of Opportunity field on Project is Opportunity__c so you have to query this field and use the same in assert statement.
Please update your test class as below :
Let me know if you have any issue.
Thanks,
Abhishek