Newer Version Available
URL Class
Namespace
Usage
1// Get a file uploaded through Chatter.
2ContentDocument doc = [SELECT Id FROM ContentDocument
3 WHERE Title = 'myfile'];
4// Create a link to the file.
5String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm() +
6 '/' + doc.id;
7system.debug(fullFileURL);Example
1// Create a new account called Acme that we will create a link for later.
2Account myAccount = new Account(Name='Acme');
3insert myAccount;
4
5// Get the base URL.
6String sfdcBaseURL = URL.getSalesforceBaseUrl().toExternalForm();
7System.debug('Base URL: ' + sfdcBaseURL );
8
9// Get the URL for the current request.
10String currentRequestURL = URL.getCurrentRequestUrl().toExternalForm();
11System.debug('Current request URL: ' + currentRequestURL);
12
13// Create the account URL from the base URL.
14String accountURL = URL.getSalesforceBaseUrl().toExternalForm() +
15 '/' + myAccount.Id;
16System.debug('URL of a particular account: ' + accountURL);
17
18// Get some parts of the base URL.
19System.debug('Host: ' + URL.getSalesforceBaseUrl().getHost());
20System.debug('Protocol: ' + URL.getSalesforceBaseUrl().getProtocol());
21
22// Get the query string of the current request.
23System.debug('Query: ' + URL.getCurrentRequestUrl().getQuery());URL Constructors
The following are constructors for URL.
Url(spec)
Signature
public Url(String spec)
Parameters
- spec
- Type: String
- The string to parse as a URL.
Url(context, spec)
Signature
public Url(Url context, String spec)
Parameters
Usage
1<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(protocol, host, file)
Signature
public Url(String protocol, String host, String file)
URL Methods
The following are methods for URL.
getAuthority()
Signature
public String getAuthority()
Return Value
Type: String
getCurrentRequestUrl()
Signature
public static System.URL getCurrentRequestUrl()
Return Value
Type: System.URL
Usage
An example of a URL for an entire request is https://yourInstance.salesforce.com/apex/myVfPage.apexp.
getDefaultPort()
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()
Signature
public String getFile()
Return Value
Type: String
getFileFieldURL(entityId, fieldName)
Signature
public static String getFileFieldURL(String entityId, String fieldName)
Parameters
Return Value
Type: String
Usage
Example:
Example
1String fileURL =
2 URL.getFileFieldURL(
3 '087000000000123' ,
4 'AttachmentBody');getHost()
Signature
public String getHost()
Return Value
Type: String
getOrgDomainUrl()
Signature
public static System.Url getOrgDomainUrl()
Return Value
Type: System.URL
getOrgDomainUrl() always returns the same domain for your org, regardless of context. Use this method to build links to record URLs that work both in Lightning Experience and in Salesforce Classic, or as the domain when making API calls to your org.
Usage
Use getOrgDomainUrl() to interact with Salesforce REST and SOAP APIs in Apex code. Get endpoints for User Interface API calls, for creating and customizing picklist value sets and custom fields, and more.
getOrgDomainUrl() can access the domain URL only for the org in which the Apex code is running.
You don't need a RemoteSiteSetting for your org to interact with the Salesforce APIs using domain URLs retrieved with this method. To bypass remote site settings, My Domain must be enabled in your org.
Example
This example uses the Salesforce REST API to get organization limit values. For information on limits, see Limits in the REST API Developer Guide.
getPath()
Signature
public String getPath()
Return Value
Type: String
getProtocol()
Signature
public String getProtocol()
Return Value
Type: String
getQuery()
Signature
public String getQuery()
Return Value
Type: String
Usage
Returns null if no query portion exists.
getRef()
Signature
public String getRef()
Return Value
Type: String
Usage
Returns null if no query portion exists.
getSalesforceBaseUrl()
Signature
public static System.URL getSalesforceBaseUrl()
Return Value
Type: System.URL
Returns the host name for the current connection: for example, https://MyDomainName.my.salesforce.com, https://MyDomainName.lightning.force.com, or, for orgs without My Domain enabled, https://yourInstance.salesforce.com or https://yourInstance.lightning.force.com.
getUserInfo()
Signature
public String getUserInfo()
Return Value
Type: String
Usage
Returns null if no UserInfo portion exists.
sameFile(URLToCompare)
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()
Signature
public String toExternalForm()
Return Value
Type: String