URL Class
Represents a uniform resource locator (URL) and provides access to parts of the URL. Enables access to the base URL used to access your Salesforce org.
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:
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.
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.
In API version 41.0 and later, Apex URL objects are represented by the java.net.URI type, not the java.net.URL type. The API version in which the URL object was instantiated determines the behavior of subsequent method calls to the specific instance. Salesforce strongly encourages you to use API 41.0 and later versions for fully RFC-compliant URL parsing that includes proper handling of edge cases of complex URL structures. API 41.0 and later versions also enforce that inputs are valid, RFC-compliant URL or URI strings.
See Also
The following are constructors for URL.
-
Creates a new instance of the
URLclass using the specified string representation of the URL. -
Creates a new instance of the
URLclass by parsing the specified spec within the specified context. -
Creates a new instance of the
URLclass using the specified protocol, host, and file on the host. The default port for the specified protocol is used. -
Url(protocol, host, port, file)
Creates a new instance of the
URLclass using the specified protocol, host, port, and file on the host.
Creates a new instance of the URL class using the specified string representation of the URL.
public Url(String spec)
- spec: Type: String The string to parse as a URL.
Creates a new instance of the URL class by parsing the specified spec within the specified context.
public Url(Url context, String spec)
- context: Type: URL The context in which to parse the specification.
- spec: Type: String The string to parse as a URL.
The new URL is created from the given context URL and the spec argument as described in RFC2396 “Uniform Resource Identifiers : Generic * Syntax” :
For more information about the arguments of this constructor, see the corresponding URL(java.net.URL, java.lang.String) constructor for Java.
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.
public Url(String protocol, String host, String file)
- 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.
Creates a new instance of the URL class using the specified protocol, host, port, and file on the host.
public Url(String protocol, String host, Integer port, String file)
- 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.
The following are methods for URL.
-
Returns the authority portion of the current URL.
-
Returns the URL of an entire request on a Salesforce instance.
-
Returns the default port number of the protocol associated with the current URL.
-
Returns the file name of the current URL.
-
getFileFieldURL(entityId, fieldName)
Returns the download URL for a file attachment.
-
Returns the host name of the current URL.
-
Returns the canonical URL for your org. For example,
https://MyDomainName.my.salesforce.com, where MyDomainName is a placeholder value. -
Returns the path portion of the current URL.
-
Returns the port of the current URL.
-
Returns the protocol name of the current URL, such as,
https. -
Returns the query portion of the current URL.
-
Returns the anchor of the current URL.
-
In API version 59.0 and later, this method is deprecated and versioned out. Use getOrgDomainUrl() to get the canonical URL for your org or use getCurrentRequestUrl() to get the URL of an entire request on a Salesforce instance. Returns the URL of the current connection to the Salesforce org.
-
Gets the UserInfo portion of the current URL.
-
Compares the current URL with the specified URL object, excluding the fragment component.
-
Returns a string representation of the current URL.
Returns the authority portion of the current URL.
public String getAuthority()
Type: String
Returns the URL of an entire request on a Salesforce instance.
public static System.URL getCurrentRequestUrl()
Type: System.URL
An example of a URL for an entire request is https://yourInstance.salesforce.com/apex/myVfPage.apexp.
Returns the default port number of the protocol associated with the current URL.
public Integer getDefaultPort()
Type: Integer
Returns -1 if the URL scheme or the stream protocol handler for the URL doesn’t define a default port number.
Returns the file name of the current URL.
public String getFile()
Type: String
Returns the download URL for a file attachment.
public static String getFileFieldURL(String entityId, String fieldName)
- 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.
Type: String
Example:
Returns the host name of the current URL.
public String getHost()
Type: String
Returns the canonical URL for your org. For example, https://MyDomainName.my.salesforce.com, where MyDomainName is a placeholder value.
public static System.Url getOrgDomainUrl()
Type: System.URL
getOrgDomainUrl() always returns the login URL for your org, regardless of context. Use that URL when making API calls to your org.
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.
This example uses the Salesforce REST API to get organization limit values. For information on limits, see Limits in the REST API Developer Guide.
See Also
- Lightning Aura Components Developer Guide: Making API Calls from Apex
- User Interface API Developer Guide: Get Default Values to Clone a Record
- User Interface API Developer Guide: Get Values for a Picklist Field
- User Interface API Developer Guide: User Inteface API Resources
Returns the path portion of the current URL.
public String getPath()
Type: String
Returns the port of the current URL.
public Integer getPort()
Type: Integer
Returns the protocol name of the current URL, such as, https.
public String getProtocol()
Type: String
Returns the query portion of the current URL.
public String getQuery()
Type: String
Returns null if no query portion exists.
Returns the anchor of the current URL.
public String getRef()
Type: String
Returns null if no query portion exists.
In API version 59.0 and later, this method is deprecated and versioned out. Use getOrgDomainUrl() to get the canonical URL for your org or use getCurrentRequestUrl() to get the URL of an entire request on a Salesforce instance. Returns the URL of the current connection to the Salesforce org.
public static System.URL getSalesforceBaseUrl()
Type: System.URL
Returns the URL for the current connection: for example, https://MyDomainName.my.salesforce.com or https://MyDomainName.lightning.force.com, where MyDomainName is a placeholder value.
See Also
Gets the UserInfo portion of the current URL.
public String getUserInfo()
Type: String
Returns null if no UserInfo portion exists.
Compares the current URL with the specified URL object, excluding the fragment component.
public Boolean sameFile(System.URL URLToCompare)
- URLToCompare: Type: System.URL
Type: Boolean
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.
Returns a string representation of the current URL.
public String toExternalForm()
Type: String