// 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://na1.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 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
An example of an instance URL is https://na1.salesforce.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