Get Record Type id by name in Apex – Salesforce

Reading Time: < 1 minutes

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


About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: