Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
96 views5 pages

Salesforce URL & UserInfo Methods Guide

The document summarizes URL and UserInfo methods in Apex. It lists methods from the System.URL and UserInfo classes that retrieve parts of URLs like the host, path, query string. It also provides an example of using these methods. UserInfo methods return information about the current user like name, locale, profile ID, and time zone. URL methods allow constructing and parsing standard Salesforce URLs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views5 pages

Salesforce URL & UserInfo Methods Guide

The document summarizes URL and UserInfo methods in Apex. It lists methods from the System.URL and UserInfo classes that retrieve parts of URLs like the host, path, query string. It also provides an example of using these methods. UserInfo methods return information about the current user like name, locale, profile ID, and time zone. URL methods allow constructing and parsing standard Salesforce URLs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Reference Apex Standard Classes and Methods

Method Arguments Returns Description


Example:

String fileURL =
URL.getFileFieldURL(
'087000000000123' ,
'AttachmentBody');

getSalesforceBaseUrl System.URL Returns the URL of the Salesforce instance.


For example,
https://na1.salesforce.com.

The following are instance methods for the System.URL class.

Method Arguments Return Description


getAuthority String Returns the authority portion of the current
URL.
getDefaultPort Integer Returns the default port number of the
protocol associated with the current URL.
Returns -1 if the URL scheme or the stream
protocol handler for the URL doesn't define
a default port number.

getFile String Returns the file name of the current URL.


getHost String Returns the host name of the current URL.
getPath String Returns the path portion of the current
URL.
getPort Integer Returns the port of the current URL.
getProtocol String Returns the protocol name of the current
URL. For example, https.
getQuery String Returns the query portion of the current
URL.
Returns null if no query portion exists.

getRef String Returns the anchor of the current URL.


Returns null if no query portion exists.

getUserInfo String Gets the UserInfo portion of the current


URL.
Returns null if no UserInfo portion exists.

492
Reference Apex Standard Classes and Methods

Method Arguments Return Description


sameFile System.URL Boolean Compares the current URL with the
URLToCompare specified URL object, excluding the
fragment component.
Returns true if both URL objects reference
the same remote resource; otherwise, returns
false.
For more information about the syntax of
URIs and fragment components, see
RFC3986.

toExternalForm String Returns a string representation of the


current URL.

URL Sample
In this example, the base URL and the full request URL of the current Salesforce server instance are retrieved. Next, a URL
pointing to a specific account object is created. Finally, components of the base and full URL are obtained. This example
prints out all the results to the debug log output.

// Create a new account called Acme that we will create a link for later.
Account myAccount = new Account(Name='Acme');
insert myAccount;

// Get the base URL.


String sfdcBaseURL = URL.getSalesforceBaseUrl().toExternalForm();
System.debug('Base URL: ' + sfdcBaseURL );

// Get the URL for the current request.


String currentRequestURL = URL.getCurrentRequestUrl().toExternalForm();
System.debug('Current request URL: ' + currentRequestURL);

// Create the account URL from the base URL.


String accountURL = URL.getSalesforceBaseUrl().toExternalForm() +
'/' + myAccount.Id;
System.debug('URL of a particular account: ' + accountURL);

// Get some parts of the base URL.


System.debug('Host: ' + URL.getSalesforceBaseUrl().getHost());
System.debug('Protocol: ' + URL.getSalesforceBaseUrl().getProtocol());

// Get the query string of the current request.


System.debug('Query: ' + URL.getCurrentRequestUrl().getQuery());

UserInfo Methods
The following are the system static methods for UserInfo.

493
Reference Apex Standard Classes and Methods

Name Arguments Return Type Description


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

Note: For Apex saved using


Salesforce.com API version 22.0 or earlier,
getDefaultCurrency returns null for
single currency organizations.

getFirstName String Returns the context user's first name


getLanguage String Returns the context user's language
getLastName String Returns the context user's last name
getLocale String Returns the context user's locale. For example:
String result =
UserInfo.getLocale();
System.assertEquals('en_US',
result);

getName String Returns the context user's full name. The format
of the name depends on the language preferences
specified for the organization. The format is one of
the following:
• FirstName LastName
• LastName, FirstName

getOrganizationId String Returns the context organization's ID


getOrganizationName String Returns the context organization's company name
getProfileId String Returns the context user's profile ID
getSessionId String Returns the session ID for the current session.
For Apex code that is executed asynchronously,
such as @future methods, Batch Apex jobs, or
scheduled Apex jobs, getSessionId returns
null.

As a best practice, ensure that your code handles


both cases – when a session ID is or is not available.

getTimeZone System.TimeZone Returns the current user’s local time zone.


Example:

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

494
Reference Apex Standard Classes and Methods

Name Arguments Return Type Description


System.debug(
'ID: ' +
tz.getID());

getUiTheme String Returns the default organization theme. Use


getUiThemeDisplayed to determine the theme
actually displayed to the current user.
Valid values are:

• Theme1
• Theme2
• PortalDefault
• Webstore

getUiThemeDisplayed String Returns the theme being displayed for the current
user.
Valid values are:

• Theme1
• Theme2
• PortalDefault
• Webstore

getUserEmail String Returns the current user’s email address.


Example:

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

getUserId String Returns the context user's ID


getUserName String Returns the context user's login name
getUserRoleId String Returns the context user's role ID
getUserType String Returns the context user's type
isCurrentUserLicensed String namespace Boolean Returns true if the context user has a license to
the managed package denoted by namespace.
Otherwise, returns false.
A TypeException is thrown if namespace is an
invalid parameter.

isMultiCurrencyOrganization Boolean Specifies whether the organization uses multiple


currencies

495
Reference Apex Standard Classes and Methods

Version Methods
Use the Version methods to get the version of a managed package of a subscriber and to compare package versions.

Usage
A package version is a number that identifies the set of components uploaded in a package. The version number has the format
majorNumber.minorNumber.patchNumber (for example, 2.1.3). The major and minor numbers increase to a chosen value
during every major release. The patchNumber is generated and updated only for a patch release.
A called component can check the version against which the caller was compiled using the System.requestVersion
method and behave differently depending on the caller’s expectations. This allows you to continue to support existing behavior
in classes and triggers in previous package versions while continuing to evolve the code.
The value returned by the System.requestVersion method is an instance of this class with a two-part version number
containing a major and a minor number. Since the System.requestVersion method doesn’t return a patch number, the
patch number in the returned Version object is null.
The System.Version class can also hold also a three-part version number that includes a patch number.

Constructors

Arguments Description
Integer major Creates a two-part package version using the specified major and minor
version numbers.
Integer minor

Integer major Creates a three-part package version using the specified major, minor,
and patch version numbers.
Integer minor
Integer patch

Methods
The following are instance methods for the System.Version class.

Method Arguments Return Type Description


compareTo System.Version version Integer Compares the current version with the
specified version and returns one of the
following values:
• zero if the current package version is
equal to the specified package version
• an Integer value greater than zero if the
current package version is greater than
the specified package version
• an Integer value less than zero if the
current package version is less than the
specified package version
If a two-part version is being compared to
a three-part version, the patch number is

496

You might also like