Great question!
For the sake of anyone who's doing this in Visualforce (which, Benj, I recognize you're not), here's what I would do: get a reference to the record type Id in Apex using a method similar to this: (note that I am returning a Map keyed by RecordType DeveloperName, NOT Name, because Name can always be changed/overridden in Production org, whereas DeveloperName does not change. This is an especially important best practice when developing managed packages.
// Returns a map of active, user-available RecordType IDs for a given SObjectType,
// keyed by each RecordType's unique, unchanging DeveloperName
global static Map<String, Id> GetRecordTypeIdsByDeveloperName(
Schema.SObjectType token
) {
// Build a map of RecordTypeIds keyed by DeveloperName
Map<String, Id> mapRecordTypes = new Map<String, Id>();
// Get the Describe Result
Schema.DescribeSObjectResult obj = token.getDescribe();
// Obtain ALL Active Record Types for the given SObjectType token
// (We will filter out the Record Types that are unavailable
// to the Running User using Schema information)
String soql =
'SELECT Id, Name, DeveloperName '
+ 'FROM RecordType '
+ 'WHERE SObjectType = \'' + String.escapeSingleQuotes(obj.getName()) + '\' '
+ 'AND IsActive = TRUE';
List<SObject> results;
try {
results = Database.query(soql);
} catch (Exception ex) {
results = new List<SObject>();
}
// Obtain the RecordTypeInfos for this SObjectType token
Map<Id,Schema.RecordTypeInfo> recordTypeInfos = obj.getRecordTypeInfosByID();
// Loop through all of the Record Types we found,
// and weed out those that are unavailable to the Running User
for (SObject rt : results) {
if (recordTypeInfos.get(rt.Id).isAvailable()) {
// This RecordType IS available to the running user,
// so add it to our map of RecordTypeIds by DeveloperName
mapRecordTypes.put(String.valueOf(rt.get('DeveloperName')),rt.Id);
}
}
return mapRecordTypes;
}
TO use this to accomplish your need, you could do something like the following:
// CONTROLLER
public class MyController() {
public transient Id myRecordType {
public get {
if (myRecordType == null) {
myRecordType = GetRecordTypeIdsByDeveloperName(
DesiredObject__c.SObjectType
).get('Desired_Record_Type_Developer_Name');
}
return myRecordType;
} private set;
}
}
// Visualforce Page with Link to create a new record of this type
<apex:page>
<apex:commandLink value='{!URLFOR( $Action.Contact.NewContact , null, [CF00NJ0000000QIX7=Contact.Id, RecordType=myRecordType])}'/>
</apex:page>
RecordType.DeveloperNamein custom links URLs like the above? – Benj Sep 28 '12 at 17:14