Basically to avoid SOQL Limit we can use dynamic apex to get RecordTypeId . So we can get a particular record type Id of a particulat Sobject or we can get all recordType id of one sobejct in map .
Here I have created one util class which has two methods .One will give you Id of particular recordType , however we need to pass the Sobejct Name and recordType Name as parameter .
Second method will return you a map of all recordType name and related Id ,where we need to pass only sobject name .
Here I have created one util class which has two methods .One will give you Id of particular recordType , however we need to pass the Sobejct Name and recordType Name as parameter .
Second method will return you a map of all recordType name and related Id ,where we need to pass only sobject name .
public class RecordTypeUtil {
//Method to get the recordTypeId
Public static Id recordTypeId(string obj,string recName){
Id recTypeId;
if(obj!= null && recName != null){
recTypeId= Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().get(recName).getRecordTypeId();
}
return recTypeId;
}
//Method to get the map of recordType Name as key and recordTypeId as value
Public static Map<String,Id> recordTypeMap(string obj){
Map<String,Id> recTypeNameWithIdMap=new Map<String,Id>();
if(obj!= null){
for(Schema.RecordTypeInfo recInfo : Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().values()){
recTypeNameWithIdMap.put(recInfo.getName(),recInfo.getRecordTypeId());
}
}
return recTypeNameWithIdMap;
}
}
Both are static methods you can call with class name like below .
Example 1.
Id recId=RecordTypeUtil.recordTypeId('Account','Draft');
Example 2.
Map<String,Id> recTypeIdMap=RecordTypeUtil.recordTypeMap('Account');
If you want only single record type Id then you can also use below code which
will give you particular record type id of a object . Just replace object name/record type
Name of your desired .
Id recordTypeId = Schema.SObjectType.OBJECT NAME.getRecordTypeInfosByName().get('RecordType Name').getRecordTypeId();
I think this will help please suggest to improve .
No comments:
Post a Comment