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
-
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.
-
Get recordtype ID in apex without caching
Since this approach do not cache operations like
Schema.getGlobalDescribe()
andsotype.getDescribe().getRecordTypeInfosByName()
performance will not be great -
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