// 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);
// 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());
The following are constructors for URL.
public Url(String spec)
public Url(Url context, String spec)
<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.
public Url(String protocol, String host, String file)
The following are methods for URL.
public String getAuthority()
Type: String
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.
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.
public String getFile()
Type: String
public static String getFileFieldURL(String entityId, String fieldName)
Type: String
Example:
String fileURL = URL.getFileFieldURL( '087000000000123' , 'AttachmentBody');
public String getHost()
Type: String
public static System.Url getOrgDomainUrl()
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.
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.
This example uses the Salesforce REST API to get organization limit values. For information on limits, see Limits in the REST API Developer Guide.
public String getPath()
Type: String
public String getProtocol()
Type: String
public String getQuery()
Type: String
Returns null if no query portion exists.
public String getRef()
Type: String
Returns null if no query portion exists.
public static System.URL getSalesforceBaseUrl()
Type: System.URL
Returns the host name for the current connection: for example, https://yourDomain.my.salesforce.com, https://yourDomain.lightning.force.com, or, for orgs without My Domain enabled, https://yourInstance.salesforce.com or https://yourInstance.lightning.force.com.
public String getUserInfo()
Type: String
Returns null if no UserInfo portion exists.
public Boolean sameFile(System.URL URLToCompare)
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.
public String toExternalForm()
Type: String