UserInfo Class

Contains methods for obtaining information about the context user.

Namespace

System

UserInfo Methods

The following are methods for UserInfo. All methods are static.

getDefaultCurrency()

Returns the context user's default currency code for multiple currency organizations or the organization's currency code for single currency organizations.

Signature

public static String getDefaultCurrency()

Return Value

Type: String

Usage

Note

Note

For Apex saved using Salesforce API version 22.0 or earlier, getDefaultCurrency returns null for single currency organizations.

getFirstName()

Returns the context user's first name

Signature

public static String getFirstName()

Return Value

Type: String

getLanguage()

Returns the context user's language

Signature

public static String getLanguage()

Return Value

Type: String

getLastName()

Returns the context user's last name

Signature

public static String getLastName()

Return Value

Type: String

getLocale()

Returns the context user's locale.

Signature

public static String getLocale()

Return Value

Type: String

Example

String result = UserInfo.getLocale();
System.assertEquals('en_US', result);

getName()

Returns the context user's full name. The format of the name depends on the language preferences specified for the organization.

Signature

public static String getName()

Return Value

Type: String

Usage

The format is one of the following:

  • FirstName LastName
  • LastName, FirstName

getOrganizationId()

Returns the context organization's ID.

Signature

public static String getOrganizationId()

Return Value

Type: String

getOrganizationName()

Returns the context organization's company name.

Signature

public static String getOrganizationName()

Return Value

Type: String

getProfileId()

Returns the context user's profile ID.

Signature

public static String getProfileId()

Return Value

Type: String

getSessionId()

Returns the session ID for the current session.

Signature

public static String getSessionId()

Return Value

Type: String

Usage

You can use getSessionId() both synchronously and asynchronously. In asynchronous Apex (Batch, Future, Queueable, or Scheduled Apex), this method returns the session ID only when the code is run by an active, valid user. When the code is run by an internal user, such as the automated process user or a proxy user, the method returns null.

As a best practice, ensure that your code handles both cases: when a session ID is or is not available.

getTimeZone()

Returns the current user’s local time zone.

Signature

public static System.TimeZone getTimeZone()

Return Value

Type: System.TimeZone

Example

TimeZone tz = 
  UserInfo.getTimeZone();
System.debug(
  'Display name: ' + 
  tz.getDisplayName());
System.debug(
  'ID: ' + 
  tz.getID());

getUiTheme()

Returns the preferred theme for the current user. Use getUiThemeDisplayed to determine the theme actually displayed to the current user.

Signature

public static String getUiTheme()

Return Value

Type: String

The preferred theme for the current user.

Valid values include:
  • Theme1—Obsolete Salesforce theme
  • Theme2—Salesforce Classic 2005 user interface theme
  • Theme3—Salesforce Classic 2010 user interface theme
  • Theme4d—Modern “Lightning Experience” Salesforce theme
  • Theme4t—Salesforce mobile app theme
  • Theme4u—Lightning Console theme
  • PortalDefault—Salesforce Customer Portal theme
  • Webstore—Salesforce AppExchange theme

getUiThemeDisplayed()

Returns the theme being displayed for the current user.

Signature

public static String getUiThemeDisplayed()

Return Value

Type: String

The theme being displayed for the current user

Valid values include:
  • Theme1—Obsolete Salesforce theme
  • Theme2—Salesforce Classic 2005 user interface theme
  • Theme3—Salesforce Classic 2010 user interface theme
  • Theme4d—Modern “Lightning Experience” Salesforce theme
  • Theme4t—Salesforce mobile app theme
  • Theme4u—Lightning Console theme
  • PortalDefault—Salesforce Customer Portal theme
  • Webstore—Salesforce AppExchange theme

getUserEmail()

Returns the current user’s email address.

Signature

public static String getUserEmail()

Return Value

Type: String

Example

String emailAddress = 
  UserInfo.getUserEmail();
System.debug(
  'Email address: ' + 
  emailAddress);

getUserId()

Returns the context user's ID

Signature

public static String getUserId()

Return Value

Type: String

getUserName()

Returns the context user's login name.

Signature

public static String getUserName()

Return Value

Type: String

getUserRoleId()

Returns the context user's role ID.

Signature

public static String getUserRoleId()

Return Value

Type: String

getUserType()

Returns the context user's type.

Signature

public static String getUserType()

Return Value

Type: String

isCurrentUserLicensed(namespace)

Returns true if the context user has a license to any managed package denoted by the namespace. Otherwise, returns false.

Signature

public static Boolean isCurrentUserLicensed(String namespace)

Parameters

namespace
Type: String

Return Value

Type: Boolean

Usage

A TypeException is thrown if namespace is an invalid type.

isCurrentUserLicensedForPackage(packageID)

Returns true if the context user has a license to the managed package denoted by the package ID. Otherwise, returns false.

Signature

public static Boolean isCurrentUserLicensedForPackage(ID packageID)

Parameters

packageID
Type: String

Return Value

Type: Boolean

Usage

Retrieve packageIDat runtime, with the getCurrentPackageId() method. Then, use packageId to confirm that the contextual user is licensed to use that managed package.

A TypeException is thrown if packageID is an invalid type. A SystemException is thrown if packageID is the ID of an unlocked or unmanaged package, or if the contextual user doesn’t have a license to the managed package.

isMultiCurrencyOrganization()

Specifies whether the organization uses multiple currencies.

Signature

public static Boolean isMultiCurrencyOrganization()

Return Value

Type: Boolean