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

how to write a test class for retURL & override?
Hi:
I need to know which I can not find in my research is how to write a testmethod when a standard button is override which means that I have specified in my controller to override the URL.
Now the standard button is New on the Opportunity.
I am overriding the Opportunity URL, so that I can place in the Contact Name and fill it in automatically when they create a new Opp through Contact record.
In my test method I do not know how to write a testmethod for this part of my controller:
public newoppbutton(ApexPages.StandardController stdController){ string ret=ApexPages.currentPage().getParameters().get('RetURL'); ret=ret.substring(1,ret.length()); if(ret.startswith('003')){ try{ mycontact = [select id,name from contact where id=:ret]; gotcontact=true; }catch(QueryException q){ system.debug(q.getmessage()); } } }
I am getting stuck at this:
string ret=ApexPages.currentPage().getParameters().get('RetURL');
ret=ret.substring(1,ret.length());
Can someone guide me or have an example of how to write this in a testmethod please?
I did try this in a testmethod which did not work:
//Now get the contact id redir='/'+ cc.id; PageReference p2 = new PageReference(redir); system.debug('PageReference p21:' + p2); Test.setCurrentPage(p2); string ret; String redire; ret=redir.substring(1); system.debug('PageReference p21d:' + ret); Contact cn=[Select name, id, firstname, lastname,recordtype.name from Contact where id=:ret];
Which did not work when I called within my testmethod:
ApexPages.standardController sc = new ApexPages.standardController(new Opportunity());
newoppbutton controller = new newoppbutton(sc);
My page Reference in my controller is :
public PageReference init() {
String redirectUrl = '';
String recordTypeId = System.currentPageReference().getParameters().get('RecordType');
if (recordTypeId != null) {
redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1';
}else if(gotcontact) {
redirectUrl = '/006/e?CF00N70000002RNNE='+mycontact.Name+'&nooverride=1';
}else{
redirectUrl = '/006/e?nooverride=1';
}
PageReference newOpp = new PageReference(redirectUrl);
return newOpp;
}
So I have tried everything and keep failing on a attempt to de-ference a null...
Thanks
Happy Holidays to all
From your previous code it looks like you need to include a RetURL parameter. Try adding this just after you create your page reference:
customPage.getParameters().put('RetURL', '/home/home.jsp');
All Answers
You need to add the parameters you use in your apex class to your page reference in your test before you call methods:
// Setup my test page with parameters
PageReference pr = new PageReference(VF PAGE URL HERE); pr.getParameters().put('myparam', 'myparamvalue'); Test.setCurrentPageReference(pr);
Hope that helps.
Thank you for the reply...
My VF page is:
When I did your suggestion in my test class it failed with the same error Attempt to de-reference a null
From your previous code it looks like you need to include a RetURL parameter. Try adding this just after you create your page reference:
customPage.getParameters().put('RetURL', '/home/home.jsp');