Skip to content

SEL_User

Class · Group: Selectors

apex
global virtual inherited sharing class SEL_User extends SEL_Base

Extends: SEL_Base

Selector for the User SObject. Provides default field configuration and query methods for User records, including retrieval by ID, role, profile, or other fields, as well as generating aliases and community nicknames.

Since: 1.0

Example:

apex
User currentUser = new SEL_User().getCurrentUser();
User foundUser = new SEL_User().findByUsername('admin@example.com');
List<User> admins = new SEL_User().findActiveByProfileName('System Administrator');
List<User> roleUsers = new SEL_User().findByRoleId(roleIds);

See Also: SEL_Base, User


Methods

MethodDescription
global List findActiveByProfileAndPermissionSetName(String profileName, String permissionSetOrGroupName)Retrieves active users with the specified profile and permission set or group, using default fields.
global List findActiveByProfileName(String profileName)Retrieves active users with the specified profile name, using default fields.
global List findActiveUsers(Integer numberOfUsers)Retrieves a specified number of active users, using default User fields.
global User findByCompanyName(String companyName)Retrieves a user by their company name, using default User fields.
global User findByFederationId(String federationId)Retrieves a user by their federation ID, using default User fields.
global List findByProfileName(Set<String> profileNames)Retrieves a list of users by their profile names, using default User fields.
global List findByRoleId(Set<Id> roleIds)Retrieves a list of users based on a set of role IDs, using default User fields.
global List findByUsername(Set<String> userNames)Retrieves a list of users by their usernames, using default User fields.
global User findByUsername(String username)Finds a user record by username.
global Id findProfileIdByUserId(Id userId)Retrieves the Profile ID for a specific user.
global User findRandomActiveByProfileAndPermissionSetName(String profileName, String permissionSetOrGroupName)Retrieves a random active user with the specified profile and permission set or group, using default fields.
global User findRandomActiveByProfileName(String profileName)Retrieves a random active user with the specified profile name, using default fields.
global String generateAlias(String fullName)Generates an alias from a user's full name, adhering to the User.Alias field length.
global String generateUniqueCommunityNickname(String fullName)Generates a unique community nickname from a user's full name, checking for existing nicknames.
global String generateUniqueCommunityNickname(String fullName, String> existingNicknames)Generates a unique community nickname from a user's full name, ensuring uniqueness against existing nicknames.
global User getCurrentUser()Retrieves the current user's details using default fields.
global virtual override List getFieldPaths()Returns relationship field paths for User queries.
global virtual override List getFields()Returns the core SObjectField tokens for User queries.
global Boolean isCurrentUserSystemAdmin()Checks if the current user has the System Administrator profile.
global SEL_User()Constructs a User selector with the User SObjectType.

Method Details

SEL_User

apex
global SEL_User()

Constructs a User selector with the User SObjectType.

Since: 1.0

Example:

apex
SEL_User instance = new SEL_User();

findActiveByProfileAndPermissionSetName

apex
global List<User> findActiveByProfileAndPermissionSetName(String profileName, String permissionSetOrGroupName)

Retrieves active users with the specified profile and permission set or group, using default fields.

Parameters:

  • profileName (String) - The name of the profile to filter by.
  • permissionSetOrGroupName (String) - The name of the permission set or group to filter by.

Returns: User - A list of active User records matching both criteria, or an empty list if none found.

Since: 1.0

Example:

apex
List<User> hrAdmins = new SEL_User().findActiveByProfileAndPermissionSetName('HR Admin', 'HR_Permissions');

findActiveByProfileName

apex
global List<User> findActiveByProfileName(String profileName)

Retrieves active users with the specified profile name, using default fields.

Parameters:

  • profileName (String) - The name of the profile to filter by.

Returns: User - A list of active User records matching the profile, or an empty list if none found.

Since: 1.0

Example:

apex
List<User> admins = new SEL_User().findActiveByProfileName('System Administrator');

findActiveUsers

apex
global List<User> findActiveUsers(Integer numberOfUsers)

Retrieves a specified number of active users, using default User fields.

Parameters:

  • numberOfUsers (Integer) - The maximum number of users to return.

Returns: User - A list of active User records, up to the specified number, or an empty list if none found.

Since: 1.0

Example:

apex
List<User> activeUsers = new SEL_User().findActiveUsers(5);

findByCompanyName

apex
global User findByCompanyName(String companyName)

Retrieves a user by their company name, using default User fields.

Parameters:

  • companyName (String) - The company name to query.

Returns: User - A User record matching the company name, or null if not found.

Since: 1.0

Example:

apex
User testUser = new SEL_User().findByCompanyName('my-test-user-identifier');

findByFederationId

apex
global User findByFederationId(String federationId)

Retrieves a user by their federation ID, using default User fields.

Parameters:

  • federationId (String) - The federation ID to query.

Returns: User - A User record matching the federation ID, or null if not found.

Since: 1.0

Example:

apex
User user = new SEL_User().findByFederationId('fed123456');

findByProfileName

apex
global List<User> findByProfileName(Set<String> profileNames)

Retrieves a list of users by their profile names, using default User fields.

Parameters:

  • profileNames (Set) - The set of profile names to query.

Returns: User - A list of User records matching the profile names, or an empty list if none found.

Since: 1.0

Example:

apex
Set<String> profileNames = new Set<String>{'System Administrator', 'Standard User'};
List<User> users = new SEL_User().findByProfileName(profileNames);

findByRoleId

apex
global List<User> findByRoleId(Set<Id> roleIds)

Retrieves a list of users based on a set of role IDs, using default User fields.

Parameters:

  • roleIds (Set) - The set of role IDs to query.

Returns: User - A list of User records assigned to the specified roles, or an empty list if none found.

Since: 1.0

Example:

apex
Set<Id> roleIds = new Set<Id>{'00E3X00000R1AbC'};
List<User> users = new SEL_User().findByRoleId(roleIds);

findByUsername

apex
global List<User> findByUsername(Set<String> userNames)

Retrieves a list of users by their usernames, using default User fields.

Parameters:

  • userNames (Set) - The set of usernames to query.

Returns: User - A list of User records matching the usernames, or an empty list if none found.

Since: 1.0

Example:

apex
Set<String> userNames = new Set<String>{'test.user@example.com'};
List<User> users = new SEL_User().findByUsername(userNames);

findByUsername

apex
global User findByUsername(String username)

Finds a user record by username.

Parameters:

  • username (String) - The username (email format) to search for

Returns: User - The User record with default fields, or null if not found

Since: 1.0

Example:

apex
User foundUser = new SEL_User().findByUsername('test.user@example.com');

findProfileIdByUserId

apex
global Id findProfileIdByUserId(Id userId)

Retrieves the Profile ID for a specific user.

Parameters:

  • userId (Id) - The user ID to look up

Returns: Id - The user's Profile ID, or null if user not found

Since: 1.0

Example:

apex
Id profileId = new SEL_User().findProfileIdByUserId(someUserId);

findRandomActiveByProfileAndPermissionSetName

apex
global User findRandomActiveByProfileAndPermissionSetName(String profileName, String permissionSetOrGroupName)

Retrieves a random active user with the specified profile and permission set or group, using default fields.

Parameters:

  • profileName (String) - The name of the profile to filter by.
  • permissionSetOrGroupName (String) - The name of the permission set or group to filter by.

Returns: User - A random active User record matching both criteria, or null if none found.

Since: 1.0

Example:

apex
User randomHrAdmin = new SEL_User().findRandomActiveByProfileAndPermissionSetName('HR Admin', 'HR_Permissions');

findRandomActiveByProfileName

apex
global User findRandomActiveByProfileName(String profileName)

Retrieves a random active user with the specified profile name, using default fields.

Parameters:

  • profileName (String) - The name of the profile to filter by.

Returns: User - A random active User record matching the profile, or null if none found.

Since: 1.0

Example:

apex
User randomAdmin = new SEL_User().findRandomActiveByProfileName('System Administrator');

generateAlias

apex
global String generateAlias(String fullName)

Generates an alias from a user's full name, adhering to the User.Alias field length.

Parameters:

  • fullName (String) - The user's full name (including first, middle, last names, prefixes, or suffixes).

Returns: String - A generated alias string, or an empty string if the input is blank.

Since: 1.0

Example:

apex
String alias = SEL_User.generateAlias('Jason van Thor Bob');

generateUniqueCommunityNickname

apex
global String generateUniqueCommunityNickname(String fullName)

Generates a unique community nickname from a user's full name, checking for existing nicknames.

Parameters:

  • fullName (String) - The user's full name.

Returns: String - A unique community nickname string, or a random name if the input is blank.

Since: 1.0

Example:

apex
String nickname = new SEL_User().generateUniqueCommunityNickname('Billy Bob Thornton');

generateUniqueCommunityNickname

apex
global String generateUniqueCommunityNickname(String fullName, Set<String> existingNicknames)

Generates a unique community nickname from a user's full name, ensuring uniqueness against existing nicknames.

Parameters:

  • fullName (String) - The user's full name.
  • existingNicknames (Set) - A set of existing nicknames to check against, or null to ignore.

Returns: String - A unique community nickname string, or a random name if the input is blank.

Since: 1.0

Example:

apex
Set<String> existing = new Set<String>{'BillyBobThornton'};
String nickname = SEL_User.generateUniqueCommunityNickname('Billy Bob Thornton', existing);

getCurrentUser

apex
global User getCurrentUser()

Retrieves the current user's details using default fields. Caches the result so subsequent calls within the same transaction do not re-query.

Returns: User - The User record for the current user, or null if not found.

Since: 1.0

Example:

apex
User currentUser = new SEL_User().getCurrentUser();

getFieldPaths

apex
global virtual override List<String> getFieldPaths()

Returns relationship field paths for User queries.

Returns: String - List of relationship field path strings

Since: 1.0

Example:

apex
SEL_User selector = new SEL_User();
List<String> paths = selector.getFieldPaths();
System.debug('Relationship paths: ' + paths); // ['Profile.Name']

getFields

apex
global virtual override List<SObjectField> getFields()

Returns the core SObjectField tokens for User queries.

Returns: SObjectField - List of User SObjectField tokens

Since: 1.0

Example:

apex
SEL_User selector = new SEL_User();
List<SObjectField> userFields = selector.getFields();
System.debug('User selector has ' + userFields.size() + ' core fields');

isCurrentUserSystemAdmin

apex
global Boolean isCurrentUserSystemAdmin()

Checks if the current user has the System Administrator profile.

Returns: Boolean - True if the current user is a System Administrator, false otherwise.

Since: 1.0

Example:

apex
if(new SEL_User().isCurrentUserSystemAdmin())
{
    // Current user is a System Administrator
}