Friday, December 9, 2022

Get Salesforce instance URL in Apex (Salesforce)


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.

getOrgDomainUrl() 

Returns the canonical URL for your org.
For example, https://MyDomainName.my.salesforce.com.

String orgDomainUrl=System.URL.getOrgDomainUrl().toExternalForm();
System.debug(orgDomainUrl);

getSalesforceBaseUrl() 

Returns the URL of current connection to the salesforce org.
For example, https://MyDomainName.my.salesforce.com or 
https://MyDomainName.lightning.force.com

String baseUrlSalesforce=System.URL.getSalesforceBaseUrl().toExternalForm();
System.debug(baseUrlSalesforce);

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.

Account acct = [SELECT Id FROM Account WHERE Name = 'Acme' LIMIT 1];
String fullRecordURL = System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + acct.Id;

getCurrentRequestUrl() 

Returns the URL of an entire request on a salesforce Instance.
An example of a URL for an entire request is
https://yourInstance.salesforce.com/apex/myVfPage.apexp

String currentUrlSalesforce=System.URL.getCurrentRequestUrl().toExternalForm();
System.debug(currentUrlSalesforce);

// Get the query string of the current request.
System.debug('Query: ' + System.URL.getCurrentRequestUrl().getQuery());

No comments:

Post a Comment