I'm overriding the new button on Opportunity and depending on the record type the user selects, I want to add on a parameter to auto-populate a field. The problem that I'm facing is that I'm getting stuck in a loop. I set the sfdc.override=1
Set up:
- On Opportunity over-riding the new button keeping the recordtype picker, and redirecting them to the VF page below
- From the VF page, calling the action (see below)
- Setting the redirect in the extension
Here's the URL that is built:
/006/e?FIELD_ID=VALUE&accid=001M00000XXXXX&core.apexpages.devmode.url=1&ent=Opportunity&RecordType=0124000000XXXXX&retURL=/001M00000XXXXX&save_new=1&sfdc.override=1
VF:
<apex:page standardcontroller="Opportunity" extensions="MyRedirect" action="{!route}">
</apex:page>
Extension
public class MyRedirect{
private final ApexPages.StandardController controller;
public Redirect(ApexPages.StandardController controller){
this.controller = controller;
}
public PageReference route(){
RecordType r = [select id from RecordType where SObjectType = 'Opportunity' and Name = 'ABC'];
String rtId = (String)r.id;
If(ApexPages.currentPage().getParameters().get('RecordType').left(15) == rtId.left(15)){
//buildURL
Map<String, String> params = new Map<String, String>();
params = ApexPages.currentPage().getParameters();
string url ='/006/e?';
boolean flag = false;
for(String varI : params.keyset()){
if (!flag){
String s =params.get(varI);
url += varI +'=' +s ;
flag = true;
}
else{
String s =params.get(varI);
url += '&' +varI +'=' +s ;
}
}
url+= '&FIELD_ID' + '=' + 'ADD VALUE';
PageReference pageRef = new PageReference(url);
pageref.setredirect(true);
return pageref;
}
else
return null;
}
}
References:
http://blog.jeffdouglas.com/2008/11/14/redirecting-users-to-different-visualforce-pages/
$RecordType.DeveloperName
? SHould work for text fields, for picklists - you can set default val. per record type too...&nooverride=1
. Also - add an if in the loop to stop copyingsfdc.override=1
if it does get copied.