Created
August 31, 2017 06:20
-
-
Save swapnilshrikhande/57a5266e92121ca2b0e4d94c458ff171 to your computer and use it in GitHub Desktop.
Get Record Type ID By Developer Name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns a map of active, user-available RecordType IDs for a given SObjectType, | |
// keyed by each RecordType's unique, unchanging DeveloperName | |
public static Map<String, Id> getRecordTypeMapForObjectGeneric(Schema.SObjectType token) { | |
// Do we already have a result? | |
Map<String, Id> mapRecordTypes = rtypesCache.get(token); | |
// If not, build a map of RecordTypeIds keyed by DeveloperName | |
if (mapRecordTypes == null) { | |
mapRecordTypes = new Map<String, Id>(); | |
rtypesCache.put(token,mapRecordTypes); | |
} else { | |
// If we do, return our cached result immediately! | |
return mapRecordTypes; | |
} | |
// Get the Describe Result | |
Schema.DescribeSObjectResult obj = token.getDescribe(); | |
//Check if we already queried all recordtypes. | |
if (results == null || results.isEmpty()) { | |
// Obtain ALL Active Record Types | |
// (We will filter out the Record Types that are unavailable | |
// to the Running User using Schema information) | |
String soql = 'SELECT Id, Name, DeveloperName, sObjectType FROM RecordType WHERE IsActive = TRUE'; | |
try { | |
results = Database.query(soql); | |
} catch (Exception ex) { | |
results = new List<SObject>(); | |
} | |
} | |
// Obtain the RecordTypeInfos for this SObjectType token | |
Map<Id,Schema.RecordTypeInfo> recordTypeInfos = obj.getRecordTypeInfosByID(); | |
// Loop through all of the Record Types we found, | |
// and weed out those that are unavailable to the Running User | |
for (SObject rt : results) { | |
if (recordTypeInfos.get(rt.Id) != null) { | |
if (recordTypeInfos.get(rt.Id).isAvailable()) { | |
// This RecordType IS available to the running user, | |
// so add it to our map of RecordTypeIds by DeveloperName | |
mapRecordTypes.put(String.valueOf(rt.get('DeveloperName')),rt.Id); | |
} | |
else { | |
System.debug('The record type ' + rt.get('DeveloperName') + ' for object ' + rt.get('sObjectType') + ' is not availiable for the user.'); | |
} | |
} | |
} | |
return mapRecordTypes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment