rspec/rules/S5389/apex/rule.adoc

51 lines
1.6 KiB
Plaintext
Raw Normal View History

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 16:49:39 +02:00
== 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;
}
----
2021-04-28 16:49:39 +02:00
== 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;
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]