Skip to main content

Code reviews rule: Apex Not Using My Domain Login URL

Written by David Martin
Updated yesterday

Apex Not Using My Domain Login URL

Why is this an issue?

Hardcoding Salesforce instance URLs (like https://na1.salesforce.com) instead of using My Domain URLs is problematic because:

  • Instance migration: Salesforce may move your org to a different instance, breaking hardcoded URLs

  • My Domain requirement: Salesforce requires My Domain for many features and will eventually retire instance-based URLs

  • Security concerns: Instance URLs don't benefit from My Domain's enhanced security features

This rule detects HTTP requests that use hardcoded Salesforce instance URLs in the format https://XX##.salesforce.com where XX is a two or three letter pod identifier and ## is a number.

Examples

Example of incorrect code:

public class SalesforceIntegration {
public void makeRequest() {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://na1.salesforce.com/services/data/v55.0/sobjects');
// ...
}
}

Example of correct code:

public class SalesforceIntegration {
public void makeRequest() {
HttpRequest req = new HttpRequest();
String baseUrl = URL.getOrgDomainUrl().toExternalForm();
req.setEndpoint(baseUrl + '/services/data/v55.0/sobjects');
// ...
}
}

How can I fix violations?

Replace hardcoded instance URLs with dynamic URL resolution:

  1. Use URL.getOrgDomainUrl() to get your org's My Domain URL

  2. Use Named Credentials for external callouts

  3. Store base URLs in Custom Settings or Custom Metadata if they must be configurable

When should I disable this rule?

You may want to dismiss this issue if:

  • The URL points to a sandbox or test environment that will never be migrated

  • You are calling a specific instance for a documented reason (rare)

Resources

Did this answer your question?