You need to sign in to do that
Don't have an account?
Waqas Ali
How to refer PageReference method from test class
I am new to apex, I have created a button to call the apex class through the visual force page.
Here is my visual force page code.
Here is my visual force page code.
<apex:page standardController="Opportunity" extensions="myclass" action="{!autoRun}"> </apex:page>Here is my apex class.
public class myclass { private final Opportunity o; String tmp; public myclass(ApexPages.StandardController stdController) { this.o = (Opportunity)stdController.getRecord(); } public PageReference autoRun() { String theId = ApexPages.currentPage().getParameters().get('id'); for (Opportunity o:[select id, name, AccountId, from Opportunity where id =:theId]) { //Create the Order Order odr = new Order( OpportunityId=o.id ,AccountId = o.AccountId ,Name = o.Name ,EffectiveDate=Date.today() ,Status='Draft' ); insert odr; tmp=odr.id; } PageReference pageRef = new PageReference('/' + tmp); pageRef.setRedirect(true); return pageRef; } }I want to create test class. I don't know how to refer PageReference autoRun() method from test class. Guys need help if some one can tell me about test class of this apex class.
Please try this and mark the best answer.
Best Regards,
-Vivek
All Answers
Please try below code and let me know if it will help you.
Best Regards,
-Vivek
Hi Vivek
i tried your code. Here is the error.
Error: Compile Error: Expression of type System.PageReference has no member named apexpages at line 19 column 4
How to fix this ?
Sorry if i ask some stupid thing.
Thanks
Waqas Ali
Sorry i did not mention in my last coment that Error is on this line
Please try this and mark the best answer.
Best Regards,
-Vivek
I found below issue in your mail class.
DML inside the for loop.
Please modify your class like below class:-
If you want the you can remove the String theId = ApexPages.currentPage().getParameters().get('id'); line also.
Modify your page like below. If any error will come
Then you can try below test class:-
Please see below blog how to create test classes in salesforce
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Please let us know if this will help you.
Thanks,
Amit Chaudhary
This improved code performance.
Happy coding !!