Apex · Salesforce

Apex Code – Static Factory for Record Type

Use below apex class to avoid duplicate in your org to pull record type, Profile, User, Environment, Sandbox name, Group Name, Queue Name related information.

This is very helpful to me – I was using separate apex class for each area like RecordTypeCommonUtility, ProfileCommonUtility, UserCommonUtility, EnvSandboxCommonUtility not I have combined all together as StaticFactory.

public class StaticFactory {

// Current User Related Details
 private static User currentUser;
 public static User getCurrentUserDetails() {
 if(currentUser == null) {
 currentUser = [select Id, Name, UserType, ProfileId, Profile.Name
 from User where Id = :UserInfo.getUserId()];
 }
 return currentUser;
 }
 
 // Added below method to know whether current user is community user or not.
 private static Boolean isCommunityUser = null;
 private static set<String> setCommunityUserTypes = new set<String>{'Portal', 'PowerCustomerSuccess'};
 public static Boolean currentUserisCommunityUser() {
 if(isCommunityUser == null) {
 // NOTE: Below condition check is case sensitive
 if(setCommunityUserTypes.contains(getCurrentUserDetails().UserType)) {
 isCommunityUser = true;
 }
 else {
 isCommunityUser = false;
 }
 }
 return isCommunityUser;
 }
 
 private static map<Id, User> mapUsers = new map<Id, User>();
 public static User getUserDetails(Id userId) {
 User userInstance = mapUsers.get(userId);
 if(userInstance == null) {
 userInstance = [select Id, Name
 from User where Id = :userId];
 mapUsers.put(userInstance.Id, userInstance);
 }
 return userInstance;
 } 
 
 // Current Environment (Sandbox / Production)
 private static Organization orgDetail;
 public static Organization getOrgDetail() {
 if(orgDetail == null) {
 orgDetail = [select Id, IsSandbox from Organization limit 1];
 }
 return orgDetail;
 }

public static String getEnvironment() {
 String strEnvironment = 'Production';
 if(getOrgDetail().IsSandbox) {
 strEnvironment = 'Sandbox';
 }
 return strEnvironment;
 }

// Current Environment Name (Sandbox)
 private Static String strOrgName;
 public static String getSandboxName() {
 strOrgName = Configuration__c.getInstance('Sandbox Name').Value__c;
 return strOrgName;
 }

// Current Environment Name (Sandbox)
 private Static String strUSBankOrgName = '';
 public static String getUSBankSandboxName() {
 Configuration__c objConf = Configuration__c.getInstance('USBank Sandbox Name');
 if(objConf != null) {
 strUSBankOrgName = Configuration__c.getInstance('USBank Sandbox Name').Value__c;
 }
 return strUSBankOrgName;
 }
 
 // Record Types Related Utilities - SOQL
 private static map<string, map<Id, RecordType>> mapAllRecordTypes;
 private static map<string, map<Id, RecordType>> loadRecordTypes(string objectName) {
 if(mapAllRecordTypes == null) {
 mapAllRecordTypes = new map<string, map<Id, RecordType>>();
 }
 
 if(!mapAllRecordTypes.containsKey(objectName)) {
 map<Id, Schema.RecordType> mapRecordType = new map<Id, Schema.RecordType>();
 for(RecordType recType : [select Id, Name, DeveloperName, SobjectType from RecordType where sObjectType = :objectName]) {
 mapRecordType.put(recType.Id, recType);
 }
 mapAllRecordTypes.put(objectName, mapRecordType);
 }
 return mapAllRecordTypes;
 }
 
 public static map<Id, RecordType> getRecordTypes(string objectName) {
 return (loadRecordTypes(objectName)).get(objectName);
 }
 
 public static set<Id> getRecordTypeIds(string objectName) {
 set<Id> setRecordTypeIds = new set<Id>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 setRecordTypeIds.add(recType.Id);
 }
 return setRecordTypeIds;
 }
 
 public static set<string> getRecordTypeNames(string objectName) {
 set<string> setRecordTypeNames = new set<string>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 setRecordTypeNames.add(recType.Name);
 }
 return setRecordTypeNames;
 }
 
 public static Id getRecordTypeIdByName(string objectName, string recordTypeName) {
 Id recordTypeId;
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(recType.Name == recordTypeName) {
 recordTypeId = recType.Id;
 break;
 }
 }
 return recordTypeId;
 }
 public static Id getRecordTypeIdByDevName(string objectName, string recordTypeName) {
 Id recordTypeId;
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 
 if(recType.DeveloperName == recordTypeName) {
 recordTypeId = recType.Id;
 break;
 }
 }
 return recordTypeId;
 }
 
 public static string getRecordTypeNameById(string objectName, Id recordTypeId) {
 return ((loadRecordTypes(objectName)).get(objectName)).get(recordTypeId).Name;
 }
 
 public static set<Id> getRecordTypeIdsByNames(string objectName, set<string> setRecordTypeNames) {
 set<Id> setRecordTypeIds = new set<Id>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(setRecordTypeNames.contains(recType.Name)) {
 setRecordTypeIds.add(recType.Id);
 }
 }
 return setRecordTypeIds;
 }
 public static set<Id> getRecordTypeIdsByDevNames(string objectName, set<string> setRecordTypeDevNames) {
 set<Id> setRecordTypeIds = new set<Id>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(setRecordTypeDevNames.contains(recType.DeveloperName)) {
 setRecordTypeIds.add(recType.Id);
 }
 }
 return setRecordTypeIds;
 }
 
 public static set<string> getRecordTypeNamesByIds(string objectName, set<Id> setRecordTypeIds) {
 set<string> setRecordTypeNames = new set<string>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(setRecordTypeIds.contains(recType.Id)) {
 setRecordTypeNames.add(recType.Name);
 }
 }
 return setRecordTypeNames;
 } 
 
 public static map<Id, string> getRecordTypeIdNameMapByNames(string objectName, set<string> setRecordTypeNames) {
 map<Id, string> mapRecordTypes = new map<Id, string>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(setRecordTypeNames.contains(recType.Name)) {
 mapRecordTypes.put(recType.Id, recType.Name);
 }
 }
 return mapRecordTypes;
 }
 
 public static map<string, Id> getRecordTypeNameIdMapByNames(string objectName, set<string> setRecordTypeNames) {
 map<string, Id> mapRecordTypes = new map<string, Id>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(setRecordTypeNames.contains(recType.Name)) {
 mapRecordTypes.put(recType.Name, recType.Id);
 }
 }
 return mapRecordTypes;
 }
 
 public static map<Id, string> getRecordTypeIdNameMapByIds(string objectName, set<Id> setRecordTypeIds) {
 map<Id, string> mapRecordTypes = new map<Id, string>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(setRecordTypeIds.contains(recType.Id)) {
 mapRecordTypes.put(recType.Id, recType.Name);
 }
 }
 return mapRecordTypes;
 }
 
 public static map<string, Id> getRecordTypeNameIdMapByIds(string objectName, set<Id> setRecordTypeIds) {
 map<string, Id> mapRecordTypes = new map<string, Id>();
 for(RecordType recType : (loadRecordTypes(objectName)).get(objectName).values()) {
 if(setRecordTypeIds.contains(recType.Id)) {
 mapRecordTypes.put(recType.Name, recType.Id);
 }
 }
 return mapRecordTypes;
 }
 
 
 // Queue Related Methods
 private static map<Id, Group> mapQueues;
 public static map<Id, Group> getQueueMap() {
 if(mapQueues == null || mapQueues.isEmpty()) {
 if(Test.isRunningTest()) {
 mapQueues = new map<Id, Group>([select Id, Name, Email, DoesSendEmailToMembers from Group where Type = 'Queue' limit 10]);
 }
 else {
 mapQueues = new map<Id, Group>([select Id, Name, Email, DoesSendEmailToMembers from Group where Type = 'Queue' limit 10000]); 
 }
 }
 return mapQueues;
 }

public static Group getQueueById(Id queueId) {
 return (getQueueMap()).get(queueId);
 }
 
 public static map<Id, string> getQueueIdNameMap() {
 map<Id, string> mapQueueIdName = new map<Id, string>();
 for(Group queueIterator : (getQueueMap()).values()) {
 mapQueueIdName.put(queueIterator.Id, queueIterator.Name);
 }
 return mapQueueIdName;
 }
 
 public static string getQueueNameById(Id queueId) {
 return (getQueueIdNameMap()).get(queueId);
 }

public static map<string, Id> getQueueNameIdMap() {
 map<string, Id> mapQueueNameId = new map<string, Id>();
 for(Group queueIterator : (getQueueMap()).values()) {
 mapQueueNameId.put(queueIterator.Name, queueIterator.Id);
 }
 return mapQueueNameId;
 }

public static string getQueueIdByName(string queueName) {
 return (getQueueNameIdMap()).get(queueName);
 }

private static map<String, Id> mapQueueNameId;
 public static map<String, Id> getQueueNameIdMapByNames(string sObjectName, set<String> queueNameList) {
 if(mapQueueNameId == null || !mapQueueNameId.keySet().containsAll(queueNameList)) {
 mapQueueNameId = new map<String, Id>();
 for(QueueSobject queueObj : [SELECT Id, QueueId, Queue.Name 
 FROM QueueSobject
 WHERE SobjectType = :sObjectName AND Queue.Name IN :queueNameList]){
 mapQueueNameId.put(queueObj.Queue.Name, queueObj.QueueId);
 }
 }
 return mapQueueNameId;
 }




// Public Group Related Methods
 private static map<Id, Group> mapGroups;
 public static map<Id, Group> getGroupMap() {
 if(mapGroups == null || mapGroups.isEmpty()) {
 if(Test.isRunningTest()) {
 mapGroups = new map<Id, Group>([select Id, Name from Group where Type = 'Regular' limit 10]);
 }
 else {
 mapGroups = new map<Id, Group>([select Id, Name from Group where Type = 'Regular' limit 10000]);
 }
 }
 return mapGroups;
 }

public static Group getGroupById(Id groupId) {
 return (getGroupMap()).get(groupId);
 }

public static map<Id, string> getGroupIdNameMap() {
 map<Id, string> mapGroupIdName = new map<Id, string>();
 for(Group groupIterator : (getGroupMap()).values()) {
 mapGroupIdName.put(groupIterator.Id, groupIterator.Name);
 }
 return mapGroupIdName;
 }

public static string getGroupNameById(Id groupId) {
 return (getGroupIdNameMap()).get(groupId);
 }

public static map<string, Id> getGroupNameIdMap() {
 map<string, Id> mapGroupNameId = new map<string, Id>();
 for(Group groupIterator : (getGroupMap()).values()) {
 mapGroupNameId.put(groupIterator.Name, groupIterator.Id);
 }
 return mapGroupNameId;
 }

public static string getGroupIdByName(string groupName) {
 return (getGroupNameIdMap()).get(groupName);
 }
 
}

 

 

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