function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mmaxtrammaxtra 

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

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

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');

Message Edited by XactiumBen on 12-17-2009 03:53 PM

All Answers

XactiumBenXactiumBen

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.

 

 

MMA_FORCEMMA_FORCE

Thank you for the reply...

My VF page is:

<apex:page standardController="Opportunity" extensions="newoppbutton" action="{!init}">
</apex:page>

 When I did your suggestion in my test class it failed with the same error Attempt to de-reference a null

ApexPages.StandardController controller = new ApexPages.StandardController(new Opportunity());
PageReference customPage = new PageReference('/apex/newbuttonpage');
customPage.getParameters().put('id', cc.name);
//System.currentPagereference().getParameters().put('name',cc.LastName);
newoppbutton extension = new newoppbutton(controller); //Keep failing here on Null attempt error

extension.init();

 

XactiumBenXactiumBen

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');

Message Edited by XactiumBen on 12-17-2009 03:53 PM
This was selected as the best answer
Ashok Kumar NayakAshok Kumar Nayak
anyone found solution to this ?