Apex · Salesforce

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

  1. Record Type Id by Name
  2. Record Type Name , ID map collection by object name
  3. 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;
}

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s