I'm working on rewriting a utility method that gives record type ids without using SOQL queries.
I found two ways to do this:
Schema.getGlobalDescribe().get(objName).getDescribe().getRecordTypeInfosByName().get(recTypeName).getRecordTypeId(); and
SObjectType.Account.getRecordTypeInfosByName().get('recTypeName').getRecordTypeId(); SObjectType.Contact.getRecordTypeInfosByName().get('recTypeName').getRecordTypeId(); SObjectType.Opportunity.getRecordTypeInfosByName().get('recTypeName').getRecordTypeId(); The problem with getGlobalDescribe() approach is, it is consuming an average of ~55 milliseconds of Apex CPU time where as the second approach consumes only ~3 milliseconds.
How can I get SObjectType.ObjectName from string 'ObjectName'? I couldn't find a way to achieve this without using the getGlobalDescribe.
I'm hoping to make the second way generic like
SObjectType.ObjectName.getRecordTypeInfosByName().get('recTypeName').getRecordTypeId();