Get Record Type id by name in Apex - Salesforce


Andres Canavesi
Nov 10, 2021
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 Name = :recordTypeName LIMIT 1];
        return rt.Id;
    }
      
    

How to use it

      
Id recordTypeId = getRecordTypeIdByName('Opportunity', 'My Record Type');
      
    

salesforce apex dev