Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Instead of having a user to click "New" and then select the record type before he sees my custom New page I want 3 Buttons that let him create a record of on of three potential record types.

I know there is a URLFOR syntax for that but couldn't get this to run because I can't hardcode the recordtypeIds as this will be differnt on the target org.

Is there a way to use this URLFOR method with variables for the record type or how else can I solve this?

Maybe with a custom controller action would do the job?!

share|improve this question

You can use the key prefix in conjunction with the RecordType parameter in a custom controller or extension. The basic idea is below. The developer name should be stable and not change between orgs. Also, once in production the ID doesn't change in sandboxes refreshed from production, so if you are already working with a record type that is in production you could use the ID.

You can adapt this code to be more specific or to not be an extension. The main thing is the format of the URL and the idea that the RecordType ID is retrieved in a relatively stable way.

public class BypassRecTypeController {
    private sObject rec;
    private String recTypeDevName;

    public BypassRecTypeController(ApexPages.StandardController controller) {
        rec = controller.getRecord();

        // might get this some other way...
        recTypeDevName = ApexPages.currentPage().getParameters().get('recTypeDevName');

    public PageReference bypass() {
        // Get the key prefix to use as part of the url
        // Alternatively, if you know the SObject you could have something like
        // Account.SObjectType.getDescribe() hard coded.
        Schema.DescribeSObjectResult dsr = rec.getSObjectType().getDescribe();
        String keyPrefix = dsr.getKeyPrefix();

        // Get the record type to add on to the query string
        Id recTypeId = [Select Id From RecordType Where DeveloperName = :recTypeDevName].Id;

        PageReference pageRef = new PageReference('/' + keyPrefix + '/e?RecordType=' + recTypeId);

        return pageRef;

share|improve this answer
But how do I pass the id and name of a parent object with you solution. I was not able to generate those cryptic parts of the URL that the Record Type Selection page or URLFOR produces. – Robert Sösemann Feb 3 '13 at 16:54
Ah...that's a bit more work, if I understand what you want correctly you are trying to pre-populate a parent lookup field on a standard page layout. It is not 100% straightforward. See this question about pre-populating standard page layouts. If you go this route and have the IDs in a custom setting, you could just put the record type Ids in there as well and access them through the $Setup Global Variable. – Peter Knolle Feb 3 '13 at 19:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.