Do not hard code RecordType - El Toro - Find articles about Visualforce, Apex, and Salesforce in general

Print Preview

Do not hard code RecordType

You are probably aware that is a bad idea to hard code IDs for records in the Apex code, because the IDs will change between sandboxes, or sandbox to production. But how can you work with RecordType IDs?

There is a table called "RecordType" that can help you avoid hardcoding those IDs. This simple code shows you how to work with this table:

String MainteanceRecordTypeID = [SELECT id FROM RecordType WHERE SobjectType='Opportunity' AND name = 'Maintenance' LIMIT 1].ID;
List<Opportunity> ops = [SELECT ID FROM Opportunity WHERE RecordTypeID = :MainteanceRecordTypeID];

Record types can also be found like this:

System.debug([SELECT Id FROM RecordType WHERE SobjectType='Contact' AND name = 'Student' ][0].Id);

comments powered by Disqus

© El Toro . IT @ 2013
Andrés Pérez