Get Record Type id by name in Apex - Salesforce

SOQL query to get a record type by name to use it in Apex code


Andres Canavesi
Nov 10, 2021
Based on this query we are going to build a method that gets the record type id by its name
featured image
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