Welcome to my blog

I have been working with Salesforce for quite a while, so don’t hesitate to contact me if you have any questions or want some advice.

s f

Subscribe
Follow Us
h

Get Record Type ID without SOQL Query

 . Apex  . Get Record Type ID without SOQL Query

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;
}

Post a Comment