Salesforce IDs and URL Tricks and Tips

Salesforce IDs Tricks:

url's and id's

Get the current User ID in an Apex.

String userId = UserInfo.getUserId();
System.debug(‘userId: ‘+userId);

Get the Salesforce Org ID in Apex without query.

String OrgId = UserInfo.getOrganizationId();
System.debug(‘OrgId: ‘+OrgId);

Get the Id from URL in visual force page to apex.

String currentRecordId = ApexPages.currentPage().getparameters().get(‘ID’);

Get the current user Session Id.

String MySessionID = UserInfo.getSessionID();
System.debug(‘MySessionID: ‘+MySessionID);

Salesforce URL Tricks: 

Get the Namespace prefix of the organization.

String orgNamespace = [SELECT NamespacePrefix FROM Organization].NamespacePrefix;
System.debug(‘orgNamespace: ‘+orgNamespace);

Get the Installed package namespace prefix.

String pkgNamespace =[select NamespacePrefix from ApexClass where Name =:’Any class from your package’].NamespacePrefix;
System.debug(‘pkgNamespace: ‘+pkgNamespace);

Get the base URL of Org.

url baseURL = URL.getSalesforceBaseUrl();
System.debug(‘Base URL: ‘ + baseURL );

Get the base URL in String. Returns a string representation of the above URL

String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
System.debug(‘Base URL: ‘ + baseURL );

Get the salesforce Host Name, Returns the host name of the current URL.

String baseURL = URL.getSalesforceBaseUrl().getHost();
System.debug(‘Base URL: ‘ + baseURL );

Create the sample account URL from the base URL.

Account myAccount = new Account(Name=’Acme’);
Insert myAccount;
String accountURL = URL.getSalesforceBaseUrl().toExternalForm() +’/’ + myAccount.Id;
System.debug(‘URL of a Acme account: ‘ + accountURL);

 Get the Protocol

System.debug(‘Protocol: ‘ + URL.getSalesforceBaseUrl().getProtocol());

 Get the query string of the current request.

System.debug(‘Query: ‘ + URL.getCurrentRequestUrl().getQuery());


1 thought on “Salesforce IDs and URL Tricks and Tips”

Leave a Comment

Your email address will not be published. Required fields are marked *

Select Language »