Skip to content

Instantly share code, notes, and snippets.

@dhoechst
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhoechst/85a37afef1f2428a3f56 to your computer and use it in GitHub Desktop.
Save dhoechst/85a37afef1f2428a3f56 to your computer and use it in GitHub Desktop.
Creating a Dispatcher
<apex:page standardController="Opportunity"
extensions="OpportunityNewDispatcherController"
action="{!redirect}">
</apex:page>
public class OpportunityNewDispatcherController{
private final ApexPages.StandardController controller;
public OpportunityNewDispatcherController(ApexPages.StandardController stdController) {
this.controller = stdController;
}
public PageReference redirect() {
PageReference pr;
String rtId = ApexPages.currentPage().getParameters().get('RecordType');
if (rtId == null) {
// If a record type wasn't passed in, we need to determine the user's default
List<Schema.RecordTypeInfo> infos = Schema.SObjectType.Opportunity.RecordTypeInfos;
//check each one
for (Schema.RecordTypeInfo rti : infos) {
if (rti.isDefaultRecordTypeMapping()) {
rtId = rti.getRecordTypeId();
}
}
}
RecordType recordType = [select DeveloperName from RecordType where Id = :rtId];
if (recordType.DeveloperName == 'Special_RT') {
// send the user to a VF page
pr = Page.SomeVFPage;
} else {
Schema.DescribeSObjectResult r = Opportunity.SObjectType.getDescribe();
pr = new PageReference('/' + r.getKeyPrefix() + '/e');
}
pr.getParameters().putAll(ApexPages.currentPage().getParameters());
// add the nooverride parameter to keep us out of an endless loop
pr.getParameters().put('nooverride', '1');
// For some reason the save_new parameter is sent which tries to autosave the page
pr.getParameters().remove('save_new');
return pr.setRedirect(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment