Using absolute URLs to Salesforce Pages is bug prone. Different sandboxes and production environments will have different instance names (like
"na10", "na15" etc.). Code using absolute URLs will only work when it runs in the corresponding salesforce instances. It will break as soon as it is
deployed in another one. Thus only relative URLs, i.e. without the domain and subdomain names, should be used when pointing to a salesforce page.
This rule raises an issue when a string matches the regular expression:
{noformat}
(?<!\w)(login|test|(dns|test|[a-z]{1,2})\d++)\.(salesforce|force|visual\.force|content\.force)\.com(?!\w)
{noformat}
Noncompliant code example
public PageReference testRedirect() {
String strUrl ='https://na8.salesforce.com/TestVFPage?AcoountId=999'; // Noncompliant
PageReference newUrl = new PageReference(strUrl);
newURL.setRedirect(true);
return newURL;
}
Compliant solution
public PageReference testRedirect() {
String strUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/TestVFPage?AcoountId=999';
PageReference newUrl = new PageReference(strUrl);
newURL.setRedirect(true);
return newURL;
}
OR
public PageReference testRedirect() {
String strUrl = URL.getOrgDomainUrl().toExternalForm() + '/TestVFPage?AcoountId=999';
PageReference newUrl = new PageReference(strUrl);
newURL.setRedirect(true);
return newURL;
}