Salesforce: getting a profile id by the name

Often we need to get a profile id for specified profiles and reuse it from different context. And usually it looks like this

[code language=”sql”]

profile id = [SELECT Id FROM Profile WHERE Name = ‘System Administrator’];

[/code]

And you can find such code in a few different places of your codebase, it will be compounded if your team has more than 1 developer. Therefore such code should be moved to utility classes. Below you can find solution for getting a profile id by the name.

[code language=”java”] private static Map<String, Id> profileIdByName = new Map<String, Id>(); public static Id getProfileIdByName(String profileName) { if (String.isBlank(profileName)) { throw new CommonException(System.Label.MethodHasGotIncorrectArgument); } if (profileIdByName.containsKey(profileName)) { return profileIdByName.get(profileName); } else { try { Id profileId = [SELECT Id FROM Profile WHERE name =: profileName].Id; profileIdByName.put(profileName, profileId); return profileId; } catch(QueryException qEx) { System.debug(‘ERROR:’ + qEx.getMessage() + ‘, in line’ + qEx.getLineNumber()); throw new CommonException(System.Label.ProfileNotFound); } } return null; // never fire } [/code]

Unit tests:

[code language=”java”] @isTest static void getProfileIdByNameWhenArgumentIsEmpty() { try { Utils.getProfileIdByName(”); } catch(CommonException ex) { System.assertEquals(System.Label.MethodHasGotIncorrectArgument, ex.getMessage()); } }

@isTest static void getProfileIdByNameWhenProfileDoesNotExist() { try { Utils.getProfileIdByName(‘123321123’); } catch(CommonException ex) { System.assertEquals(System.Label.ProfileNotFound, ex.getMessage()); } }

@isTest static void getProfileIdByNameWhenQueredExistingKey() { Id profileId = Utils.getProfileIdByName(GlobalConstants.PROFILE_SYSTEM_ADMINISTRATOR); System.assertEquals(1, Limits.getQueries()); System.assertEquals([SELECT Id FROM Profile WHERE Name =:GlobalConstants.PROFILE_SYSTEM_ADMINISTRATOR].Id, profileId); Utils.getProfileIdByName(GlobalConstants.PROFILE_SYSTEM_ADMINISTRATOR); Utils.getProfileIdByName(GlobalConstants.PROFILE_SYSTEM_ADMINISTRATOR); System.assertEquals(2, Limits.getQueries()); }

@isTest static void getProfileIdByNameWhenQueredForFewKeys() { Id profileId = Utils.getProfileIdByName(GlobalConstants.PROFILE_SYSTEM_ADMINISTRATOR); System.assertEquals(1, Limits.getQueries()); System.assertEquals([SELECT Id FROM Profile WHERE Name =:GlobalConstants.PROFILE_SYSTEM_ADMINISTRATOR].Id, profileId); System.assertEquals(2, Limits.getQueries()); profileId = Utils.getProfileIdByName(GlobalConstants.PROFILE_BRANCH_STANDARD_PLATFORM_USER); System.assertEquals(3, Limits.getQueries()); System.assertEquals([SELECT Id FROM Profile WHERE Name =:GlobalConstants.PROFILE_BRANCH_STANDARD_PLATFORM_USER].Id, profileId); System.assertEquals(4, Limits.getQueries()); Utils.getProfileIdByName(GlobalConstants.PROFILE_BRANCH_STANDARD_PLATFORM_USER); Utils.getProfileIdByName(GlobalConstants.PROFILE_SYSTEM_ADMINISTRATOR); System.assertEquals(4, Limits.getQueries()); } [/code]

[apex][force.com]