How to get RecordType Id efficiently in Apex with caching

It is very common needing to get recordtypeId when we are working in Salesforce. There are multiple ways to do it. Here we will go through different ways and find the best way.

Different approaches

  1. SOQL against RecordType standard object

    [SELECT Id, Name FROM RecordType WHERE Name = 'Master' AND SObjectType = 'Account']

    This approach count against the number of SOQL queries.

  2. Get recordtype ID in apex without caching

    Since this approach do not cache operations like Schema.getGlobalDescribe() and sotype.getDescribe().getRecordTypeInfosByName() performance will not be great

  3. Get recordtype ID in apex with caching USE THIS APPROACH

    Here we cache expensive schema describe operations. Allowing multiple calls to the same method to perform efficient and fast in same transaction.

    You can get recordtype Id using the syntax RecordTypeUtilWithCache.getRecordTypeId('Account', 'RecordTypeNameHere');

No comments:

Post a Comment