Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:33:32+00:00 2026-06-07T17:33:32+00:00

I have some custom fields on my User object that I want to access

  • 0

I have some custom fields on my User object that I want to access with APEX code in my VisualForce trigger. When I access it from a Formula field I get to use a nifty $User reference like this:

$User.my_prop__c

From APEX I have to query the User object by UserId like this:

[select my_prop__c from User where id = :UserInfo.getUserId()].my_prop__c;

Is there something baked into APEX already that would let me get at the user properties without the SOQL query? If not, does anyone know of a utility class for lazy loading and caching user properties so the overhead is minimal.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:33:34+00:00Added an answer on June 7, 2026 at 5:33 pm

    I would use something similar to the following code sample. It uses a singleton pattern to statically store the information in memory for the duration of your transaction. It’s similar to the lazy loading that twamley proposed but I feel this is a much simpler approach.

    Usage 1: UserUtil.CurrentUser.Email;

    Usage 2: User someUser = UserUtil.getUser(someUserId);

    This will allow you to access the same information on the current user or other users in the system. Notice the queryUsers method just returns a query result. This makes it easy to add and remove fields from your query as it is isolated in its own method keeping things simple.

    Note: that this code pulls in all users when used. Most orgs do not have multiple hundreds of users so heap size shouldn’t be a concern. But if it is you can just modify the queryUsers() method to only return active users or filter down based on other criteria.

    public class UserUtil {
    //Protected Members
    private static final UserUtil instance = new UserUtil();
    private Map<Id, User> mapUsers;
    
    //Properties
    public static User CurrentUser {
        get { return getUser(UserInfo.getUserId()); }
    }
    
    //Constructor
    private UserUtil() {
        mapUsers = new Map<Id, User>(queryUsers());
    }
    
    //Public Methods
    public static User getUser(Id userId) {
        if (instance.mapUsers.containsKey(userId)) {
            return instance.mapUsers.get(userId);
        }
        else {
            throw new InvalidUserIdException('Unable to locate user id: ' + userId);
        }
    }
    
    //Private Methods
    private List<User> queryUsers() {
        return [SELECT 
                    Id
                    , Name
                    , UserName
                    , Email
                    , Alias 
                FROM 
                    User];
    }
    
    //Internal Classes
    public class InvalidUserIdException extends Exception {}
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET project which has already some custom fields in it's web.config
I have some custom logic that needs to be executed every single time a
I have some existing custom Excel workbooks/applications with code-behind C#, which work fine. They
I have authored some custom classes that I would like to create using XAML:
I have a Service that downloads a file from the internet. What I want
I have a Django app with custom form fields, some of which have slow
I have an architectural question concerning custom fields in a view for an object.
I have created an object called Project that has different properties (strings and some
I have a user control with some custom client side validation. When I have
I have created a custom user control that is meant to extend the functionality

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.