Based on this query we are going to build a method that gets the record type id by its name
SELECT Id, Name, DeveloperName FROM RecordType WHERE SobjectType= 'Opportunity'
The method
static Id getRecordTypeIdByName(String sobjectType, String recordTypeName){
RecordType rt = [SELECT Id FROM RecordType WHERE SobjectType= :sobjectType AND DeveloperName = :recordTypeName LIMIT 1];
return rt.Id;
}
How to use it
Id recordTypeId = getRecordTypeIdByName('Opportunity', 'My Record Type');
Photo by Bruno Martins on Unsplash