URL Methods

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

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;

Constructors

Arguments Description
Default constructor. No arguments. Creates a new instance of the System.URL class.
String protocol

String host

Integer port

String file

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

String host

String file

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

String spec

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

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

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

Methods

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

Method Returns Description
getCurrentRequestUrl System.URL Returns the URL of an entire request on a Salesforce instance.

For example, https://na1.salesforce.com/apex/myVfPage.apexp.

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.

sameFile System.URLURLToCompare Boolean Compares the current URL with the 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());
© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.