Below mentioned code helps in getting the record type id by the name. Pass the object name and the record type name as an input parameter to the function and get the record type id as an output
public static id getRecordTypeIdByName(string objectAPIName, string recordTypeName){
id recordTypeId;
Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
Map<String,Id> mapOfRecordTypeNameandId = new Map<String,Id>();
for(Schema.RecordTypeInfo info : recordTypeInfo){
mapOfRecordTypeNameandId.put(info.getDeveloperName(), info.getRecordTypeId());
}
recordTypeId = mapOfRecordTypeNameandId.get(recordTypeName);
system.debug(recordTypeId);
return recordTypeId;
}
Hope this helps.
public static id getRecordTypeIdByName(string objectAPIName, string recordTypeName){
id recordTypeId;
Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
Map<String,Id> mapOfRecordTypeNameandId = new Map<String,Id>();
for(Schema.RecordTypeInfo info : recordTypeInfo){
mapOfRecordTypeNameandId.put(info.getDeveloperName(), info.getRecordTypeId());
}
recordTypeId = mapOfRecordTypeNameandId.get(recordTypeName);
system.debug(recordTypeId);
return recordTypeId;
}
Hope this helps.
0 Comments