Get Record Type ID by Name

Sample Code:


        String strObject = ‘Account’;
        String rtName = ‘Marketing’;

        Schema.DescribeSObjectResult d = getDescribSObject(strObject);        
        Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
        Schema.RecordTypeInfo rtByName = rtMapByName.get(rtName);
        Id recTypeId = rtByName.getRecordTypeId();



        public static Schema.DescribeSObjectResult getDescribSObject(String strObject) {
            Schema.DescribeSObjectResult d;
            if(strObject.equals(‘Account’)) {
                        d = Schema.SObjectType.Account;
                }
        }


(Or)


Sample Code:



Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get(‘Test’).getRecordTypeId();

(Or)


Sample Code:


Id devRecordTypeId = Schema.getGlobalDescribe().get(‘Account’).getDescribe().getRecordTypeInfosByName().get(‘Test’).getRecordTypeId();


Cheers!!!

Leave a Reply