How to get record type id by name in the salesforce apex

Below mentioned code helps in getting the record type id by the name. Pass the object name and the record type name as an input parameter to the function and get the record type id as an output

    public static id getRecordTypeIdByName(string objectAPIName, string recordTypeName){
        id recordTypeId;
        Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
        List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
        Map<String,Id> mapOfRecordTypeNameandId = new Map<String,Id>();
        for(Schema.RecordTypeInfo info : recordTypeInfo){
            mapOfRecordTypeNameandId.put(info.getDeveloperName(), info.getRecordTypeId());
        }
        recordTypeId = mapOfRecordTypeNameandId.get(recordTypeName);
        system.debug(recordTypeId);
        return recordTypeId;

    }

Hope this helps. 

Post a Comment

0 Comments