Get Record Type ID without SOQL Query
Many scenario’s developers hard coding record type id or name in the apex class or test class. Please refer below sample code to get id or name without SOQL query
- Record Type Id by Name
- Record Type Name , ID map collection by object name
- Record Type Id, Name map collection by object name
Common Utility Sample Code :
public static Schema.DescribeSObjectResult getDescribSObject(String strObject) { Schema.DescribeSObjectResult d; if(strObject.equals(‘Account’)) { d = Schema.SObjectType.Account; } return d; } public static Id getRedordTypeIdByName(String rtName, String strObject) { Schema.DescribeSObjectResult d = getDescribSObject(strObject); Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtByName = rtMapByName.get(rtName); return rtByName.getRecordTypeId(); } public static map<Id, String> getsObjectRecordTypeIdKeyset(String strObject) { Schema.DescribeSObjectResult d = getDescribSObject(strObject); map<Id, String> mapDFFRT = new map<Id, String>(); for(RecordTypeInfo rTypeInfo : d.getRecordTypeInfos()) { mapDFFRT.put(rTypeInfo.getRecordTypeId(), rTypeInfo.getName()); } return mapDFFRT; } public static map<String, Id> getsObjectRecordTypeNameKeyset(String strObject) { Schema.DescribeSObjectResult d = getDescribSObject(strObject); map<String, Id> mapDFFRT = new map<String, Id>(); for(RecordTypeInfo rTypeInfo : d.getRecordTypeInfos()) { mapDFFRT.put(rTypeInfo.getName(), rTypeInfo.getRecordTypeId()); } return mapDFFRT; }
0 Comments