rspec/rules/S5390/apex/rule.adoc

55 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Its a best practice to avoid hardcoding of text strings in Apex. Ideally those text should be stored in a Custom Label and referenced via those in the code. This enables administrators and translators to update the messages without modifying the code. It also enables the translation of those text in international organizations.
This rule raises an issue when a call to the ``++sObject.addError++`` method is given a hardcoded string.
=== 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 class MyClass{
public static myMethod(Case mycase) {
mycase.addError('This is a hardcoded error');
}
}
----
=== 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 class MyClass{
public static myMethod(Case case) {
mycase.addError(System.Label.caseErrorMessage);
}
}
----
== Resources
2021-04-28 16:49:39 +02:00
* https://help.salesforce.com/articleView?id=cl_about.htm&type=5[Custom Labels]
* https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_methods_system_sobject.htm[sObject Class]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace this hardcoded message with a Label
=== Highlighting
The hardcoded string
endif::env-github,rspecator-view[]