2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
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{plus}{plus})\.(salesforce|force|visual\.force|content\.force)\.com(?!\w)
{noformat}
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
public PageReference testRedirect() {
String strUrl ='https://na8.salesforce.com/TestVFPage?AcoountId=999'; // Noncompliant
PageReference newUrl = new PageReference(strUrl);
newURL.setRedirect(true);
return newURL;
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
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;
}
----
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Make this absolute URL relative.
=== Highlighting
The URL String
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 6 Aug 2019, 10:46:28 Alban Auzeill wrote:
\[~nicolas.harraudeau],
I disagree with the regular expression (http|https)://[^/]\.(salesforce|force)\.com.*
IMO it's too generic (and by the way wrong because [^/] match only one character), we should only report issues for server hostnames that belong to a group, for example:
{noformat}
dns03.salesforce.com
dns06.salesforce.com
ns2.salesforce.com
ns4.salesforce.com
https://cs1.salesforce.com
https://cs2.salesforce.com
https://c.cs3.visual.force.com
https://c.cs4.visual.force.com
https://na4.salesforce.com
https://ap7.salesforce.com
https://eu9.salesforce.com
https://cs12.salesforce.com
https://my-domain-dev-ed--c.na50.content.force.com/
test1.force.com
test3.force.com
https://login.salesforce.com (production)
https://test.salesforce.com (sandbox)
{noformat}
But not if the server hostname does not belong to a group, for example:
{noformat}
www.salesforce.com
admin.salesforce.com
blog.salesforce.com
community.salesforce.com
developers.salesforce.com
downloads.salesforce.com
info.salesforce.com
mail.salesforce.com
mobile.salesforce.com
support.salesforce.com
video.salesforce.com
mydomain.my.salesforce.com
m.force.com
error.force.com
payment.force.com
directory.force.com
database.force.com
redhat.force.com
cisco.force.com
dell.force.com
{noformat}
I prefer a more specific expression compliant with the above lists:
{noformat}
(?<!\w)(login|test|(dns|test|[a-z]{1,2})\d{plus}{plus})\.(salesforce|force|visual\.force|content\.force)\.com(?!\w)
{noformat}
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]