Get Record Type ID without using a query SOQL call

Treat SOQL calls like gold dust.  With complex triggers it is easy to approach and breach the SOQL count limit so it important to ensure code is optimised to reduce SOQL calls and generally be as efficient as possible.  Also, it is not a good idea to hardcode record ids in your code.

A great win, to reduce SOQL calls, is to get Record Types ids by using a describe method.  (This method counts against another limit but saves a more valuable SOQL call.)

YourObjectName.SObjectType.getDescribe().getRecordTyeInfosByName().get('YourRecTypeName').getRecordTypeId();

eg:

id accountMasterRecID = Account.SObjectType.getDescribe().getRecordTyeInfosByName().get('Master').getRecordTypeId();

Salesforce documentation here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm

 

This entry was posted in Salesforce Top Tips and tagged , . Bookmark the permalink.