Represents a uniform resource locator (URL) and provides access to parts of the URL. Enables access to the Salesforce instance URL.
// 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);
| 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. |
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. |
// 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());