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.

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<User> findActiveByProfileAndPermissionSetName(String profileName, String permissionSetOrGroupName)Retrieves active users with the specified profile and permission set or group, using default fields.
global List<User> findActiveByProfileName(String profileName)Retrieves active users with the specified profile name, using default fields.
global List<User> 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<User> findByProfileName(Set<String> profileNames)Retrieves a list of users by their profile names, using default User fields.
global List<User> findByRoleId(Set<Id> roleIds)Retrieves a list of users based on a set of role IDs, using default User fields.
global List<User> 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, Set<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<String> getFieldPaths()Returns relationship field paths for User queries.
global virtual override List<SObjectField> getFields()Returns the core SObjectField tokens for User queries.
global Boolean isCurrentUserSystemAdmin()Checks if the current user has the System Administrator profile.

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

ParameterTypeDescription
profileNameStringThe name of the profile to filter by.
permissionSetOrGroupNameStringThe 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.

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

ParameterTypeDescription
profileNameStringThe 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.

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

ParameterTypeDescription
numberOfUsersIntegerThe 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.

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

ParameterTypeDescription
companyNameStringThe company name to query.

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

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

ParameterTypeDescription
federationIdStringThe federation ID to query.

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

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

ParameterTypeDescription
profileNamesSetThe set of profile names to query.

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

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

ParameterTypeDescription
roleIdsSetThe 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.

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

ParameterTypeDescription
userNamesSetThe set of usernames to query.

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

Example

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

Finds a user record by username.

Parameters

ParameterTypeDescription
usernameStringThe username (email format) to search for

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

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

ParameterTypeDescription
userIdIdThe user ID to look up

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

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

ParameterTypeDescription
profileNameStringThe name of the profile to filter by.
permissionSetOrGroupNameStringThe 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.

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

ParameterTypeDescription
profileNameStringThe name of the profile to filter by.

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

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

ParameterTypeDescription
fullNameStringThe 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.

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

ParameterTypeDescription
fullNameStringThe user's full name.

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

Example

apex
String nickname = new SEL_User().generateUniqueCommunityNickname('Billy Bob Thornton');
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

ParameterTypeDescription
fullNameStringThe user's full name.
existingNicknamesSetA 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.

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.

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

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

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.

Example

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

Constructors

ConstructorDescription
global SEL_User()Constructs a User selector with the User SObjectType.

SEL_User()

apex
global SEL_User()

Constructs a User selector with the User SObjectType.

Example

apex
SEL_User instance = new SEL_User();