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:
Use
URL.getOrgDomainUrl()to get your org's My Domain URLUse Named Credentials for external callouts
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
