URL Class

Represents a uniform resource locator (URL) and provides access to parts of the URL. Enables access to the Salesforce instance URL.

Namespace

System

Usage

Use the methods of the System.URL class to create links to objects in your organization. Such objects can be files, images, logos, or records that you want to include in external emails, in activities, or in Chatter posts. For example, you can create a link to a file uploaded as an attachment to a Chatter post by concatenating the Salesforce base URL with the file ID, as shown in the following example:
// Get a file uploaded through Chatter.
ContentDocument doc = [SELECT Id FROM ContentDocument 
          WHERE Title = 'myfile'];
// Create a link to the file.
String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm() +
   '/' + doc.id;
system.debug(fullFileURL);
The following example creates a link to a Salesforce record. The full URL is created by concatenating the Salesforce base URL with the record ID.
Account acct = [SELECT Id FROM Account WHERE Name = 'Acme' LIMIT 1];
String fullRecordURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + acct.Id;

Example

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());

URL Constructors

The following are constructors for URL.

URL(String)

Creates a new instance of the URL class using the specified string representation of the URL.

Signature

public Url(String spec)

Parameters

spec
Type: String
The string to parse as a URL.

URL(URL, String)

Creates a new instance of the URL class by parsing the specified spec within the specified context.

Signature

public Url(Url context, String spec)

Parameters

context
Type: URL
The context in which to parse the specification.
spec
Type: String
The string to parse as a URL.

Usage

The new URL is created from the given context URL and the spec argument as described in RFC2396 "Uniform Resource Identifiers : Generic * Syntax" :
<scheme>://<authority><path>?<query>#<fragment>

For more information about the arguments of this constructor, see the corresponding URL(java.net.URL, java.lang.String) constructor for Java.

URL(String, String, String)

Creates a new instance of the URL class using the specified protocol, host, and file on the host. The default port for the specified protocol is used.

Signature

public Url(String protocol, String host, String file)

Parameters

protocol
Type: String
The protocol name for this URL.
host
Type: String
The host name for this URL.
file
Type: String
The file name for this URL.

URL(String, String, Integer, String)

Creates a new instance of the URL class using the specified protocol, host, port, and file on the host.

Signature

public Url(String protocol, String host, Integer port, String file)

Parameters

protocol
Type: String
The protocol name for this URL.
host
Type: String
The host name for this URL.
port
Type: Integer
The port number for this URL.
file
Type: String
The file name for this URL.

URL Methods

The following are methods for URL.

getAuthority()

Returns the authority portion of the current URL.

Signature

public String getAuthority()

Return Value

Type: String

getCurrentRequestUrl()

Returns the URL of an entire request on a Salesforce instance.

Signature

public static System.URL getCurrentRequestUrl()

Return Value

Type: System.URL

Usage

An example of a URL for an entire request is https://na1.salesforce.com/apex/myVfPage.apexp.

getDefaultPort()

Returns the default port number of the protocol associated with the current URL.

Signature

public Integer getDefaultPort()

Return Value

Type: Integer

Usage

Returns -1 if the URL scheme or the stream protocol handler for the URL doesn't define a default port number.

getFile()

Returns the file name of the current URL.

Signature

public String getFile()

Return Value

Type: String

getFileFieldURL(String, String)

Returns the download URL for a file attachment.

Signature

public static String getFileFieldURL(String entityId, String fieldName)

Parameters

entityId
Type: String
Specifies the ID of the entity that holds the file data.
fieldName
Type: String
Specifies the API name of a file field component, such as AttachmentBody.

Return Value

Type: String

Usage

Example:

Example

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

getHost()

Returns the host name of the current URL.

Signature

public String getHost()

Return Value

Type: String

getPath()

Returns the path portion of the current URL.

Signature

public String getPath()

Return Value

Type: String

getPort()

Returns the port of the current URL.

Signature

public Integer getPort()

Return Value

Type: Integer

getProtocol()

Returns the protocol name of the current URL, such as, https.

Signature

public String getProtocol()

Return Value

Type: String

getQuery()

Returns the query portion of the current URL.

Signature

public String getQuery()

Return Value

Type: String

Usage

Returns null if no query portion exists.

getRef()

Returns the anchor of the current URL.

Signature

public String getRef()

Return Value

Type: String

Usage

Returns null if no query portion exists.

getSalesforceBaseUrl()

Returns the URL of the Salesforce instance.

Signature

public static System.URL getSalesforceBaseUrl()

Return Value

Type: System.URL

Usage

An example of an instance URL is https://na1.salesforce.com.

getUserInfo()

Gets the UserInfo portion of the current URL.

Signature

public String getUserInfo()

Return Value

Type: String

Usage

Returns null if no UserInfo portion exists.

sameFile(System.URL)

Compares the current URL with the specified URL object, excluding the fragment component.

Signature

public Boolean sameFile(System.URL URLToCompare)

Parameters

URLToCompare
Type: System.URL

Return Value

Type: Boolean

Returns true if both URL objects reference the same remote resource; otherwise, returns false.

Usage

For more information about the syntax of URIs and fragment components, see RFC3986.

toExternalForm()

Returns a string representation of the current URL.

Signature

public String toExternalForm()

Return Value

Type: String

© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.