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
Scott.MScott.M 

Override New for Single Record Type

Hi,

I don't think this is possible but I might be missing something. What I would like to do is override the New button but only for a specific Record Type.

The normal functionality is that if you have more than one record type, clicking new will take you to a first step where you select the record type and then clicking next takes you to the layout for the record type selected.

I want to keep the first step, and for all but one record type keep the standard creation pages. For the one recordtype I want to display a visualforce page when it's selected in the first step. I'm hoping I don't have to rewrite the first wizard step, any ideas?

Thanks!
Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Sorry for taking so long to come back to this, but there is a (relatively obtuse) way of doing this. What you could do is create an overriding Visualforce or S-Control (soon to be obsolete, right?) page that would determine if the user selected the record type that needs a visualforce page, and if not, then redirect them to the standard page. They'll get the usual "flash" of a blank screen before they get to their destination, but it certainly would work.

 

To get it to work, you need an extension and a page:

 

ApexPage:

 

 

 

<apex:page standardController="Account" extensions="Redirect_extension">

<script>if({!redirect}) window.top.location.href='/001/e?RecordType={!recordtypeid}&nooverride=1'</script>

<!-- Do your custom page here -->

</apex:page>

 


 

 

 

Controller:

 

 

public class Redirect_extension {

private String RecordTypeId, RecordTypeName;

public Redirect_extension(ApexPages.StandardController controller)

{ RecordTypeId = (Case)controller.getRecord().RecordTypeId;

RecordTypeName = '';

for(RecordType rt:[select id,name from recordtype where id = :recordtypeid])

RecordTypeName = rt.name;

}

public boolean getRedirect()

{ if(RecordTypeName=='My Chosen Record Type')

return false;

return true;

}

public string getRecordTypeId()

{ return RecordTypeId;

}

}

 

This is just a rough model, but it looks like it should work. Please feel free to contact me if you need any further help: phyrfoxster at gmail. 

 

Edit: Changed the URL reference from RecordTypeId to RecordType, tried to make the code legible.

Message Edited by sfdcfox on 03-06-2009 09:06 AM

All Answers

sfdcfoxsfdcfox
You can't (yet?) override the page layout with a Visualforce page for a sole record type, nor can you forcibly override the record type selection screen (the users would have to opt-in to their default record type, or you would have to limit them to one record type, in order to skip the screen entirely). I can't think of a (non-screen-scraping) way of doing this.
Scott.MScott.M
Thanks for the response,

I don't want to override the record type selection so that's okay. I tested it that and it works fine. I can over ride the new button and I still get the record type selection screen where I can select the record type and then when I hit next I get sent to the visual force page. So I guess the only thing I need to know is if I can redirect to the standard create page from a controller or controller extension?

Scott.MScott.M
So far it seems that it is not possible to override the new function with a custom visualforce page for individual record types. It's all or nothing. Please correct me if I'm wrong :) I really need to do this.
sfdcfoxsfdcfox

Sorry for taking so long to come back to this, but there is a (relatively obtuse) way of doing this. What you could do is create an overriding Visualforce or S-Control (soon to be obsolete, right?) page that would determine if the user selected the record type that needs a visualforce page, and if not, then redirect them to the standard page. They'll get the usual "flash" of a blank screen before they get to their destination, but it certainly would work.

 

To get it to work, you need an extension and a page:

 

ApexPage:

 

 

 

<apex:page standardController="Account" extensions="Redirect_extension">

<script>if({!redirect}) window.top.location.href='/001/e?RecordType={!recordtypeid}&nooverride=1'</script>

<!-- Do your custom page here -->

</apex:page>

 


 

 

 

Controller:

 

 

public class Redirect_extension {

private String RecordTypeId, RecordTypeName;

public Redirect_extension(ApexPages.StandardController controller)

{ RecordTypeId = (Case)controller.getRecord().RecordTypeId;

RecordTypeName = '';

for(RecordType rt:[select id,name from recordtype where id = :recordtypeid])

RecordTypeName = rt.name;

}

public boolean getRedirect()

{ if(RecordTypeName=='My Chosen Record Type')

return false;

return true;

}

public string getRecordTypeId()

{ return RecordTypeId;

}

}

 

This is just a rough model, but it looks like it should work. Please feel free to contact me if you need any further help: phyrfoxster at gmail. 

 

Edit: Changed the URL reference from RecordTypeId to RecordType, tried to make the code legible.

Message Edited by sfdcfox on 03-06-2009 09:06 AM
This was selected as the best answer
Scott.MScott.M

Thanks sfdcfox!

 

I tried to do that but I think I failed to put the &nooverride=1 flag. I'm assuming this will stop it from redirecting with to the override which would be awesome. I'll give it a shot :) 

 

Thanks again

sfdcfoxsfdcfox

Yes, my first attempt resulted in an infinite redirect, then I remembered that very useful parameter. Let us know how it works out for you. Note I made a small adjustment, since I actually tried to test it out (see prior post). It actually works out quite nicely, and depending on the browser, you hardly see anything before it redirects anyways, since it usually doesn't have time to render much of the page before the script runs and the redirect occurs.

 

I think there's an all-apex-code way of doing it, using PageReference objects, but, you know, I'll settle for the easy solution.

Scott.MScott.M

It works perfectly! Just that one query string parameter I was missing.

 

Here's the code I used for anyone who's interested. Just a slight variation on sfdcfox's.  I override the new and edit buttons with the visualforce page, that whey people always get routed to the right place :) 

 

 Thanks so much sfdcfox you rock! 

 

public class EventRouter {

public String redirectUrl {get; set;}


public EventRouter(ApexPages.StandardController stdController){

Id eventId = System.currentPageReference().getParameters().get('Id');
Id recordTypeId = System.currentPageReference().getParameters().get('RecordType');

RecordType rt = AtTaskUtil.getSTGProjectRecordType();
if(eventId == null) {
DescribeSObjectResult result = Schema.SObjectType.Event;
if(recordTypeId == rt.Id) {
this.redirectUrl = Page.STGProjectEvent.getUrl();
} else {
this.redirectUrl = '/'+result.getKeyPrefix()+'/e?RecordType='+recordTypeId+'&nooverride=1';
}
} else {

Event e = [SELECT Id, RecordTypeId FROM Event WHERE Id =: eventId LIMIT 1];
if(e.RecordTypeId == rt.Id) {
this.redirectUrl = Page.STGProjectEvent.getUrl()+'?id='+eventId;
} else {
this.redirectUrl = '/'+eventId+'/e?nooverride=1';
}
}

}


}


<apex:page standardController="Event" extensions="EventRouter" showHeader="false" sideBar="false" >
<script>
window.top.location.href="{!redirectUrl}";
</script>
</apex:page>

 

 

 

 

 

Message Edited by Scott.M on 03-06-2009 01:21 PM
JPlayEHRJPlayEHR

FANTASTIC TIP! That "nooverride=1" parameter is a life-saver. I was able to accomplish this same thing without an s-control using a visualforce page we already had in place overriding the new button. When we created it we only had 1 record type, but eventually added a record type that didn't need the visualforce override when creating a new record.

 

So I edited my extension class to include a pageReference redirect based on the record type and then set that as the apex:pages "action" attribute returning "null" for the record type that needed the visualforce override, and then returned the "new" URL with the "nooverride=1" parameter in it. Works great!

Y'all are awesome!

Saim JackSaim Jack
Yes, my first attempt resulted in an infinite redirect. (https://www.6pong.com/)
Siefert SiamSiefert Siam

I try it..

My 1st attempt successful (https://www.weihnachtspullover.de/)ly terminated...