You need to sign in to do that
Don't have an account?
odie123
configurable constants
Hi,
I am used to java development where I can use a constants file which I load when the application wakes up, from which I read in values during runtime. These are any constants that a system admin can configure.. like the email addresses to which notifications should go to, retry counts, url end points etc. In a Java application you modify a constants file and restart the application. How do you provide such functionality in APEX?
thanks,
Odie
Hi odie123,
Why don't you try to create a global and common class with public variables which you can update everytime you need it.
Regards,
Wilmer
Hi Wilmer,
Thank you for your response. While that would work, I was hoping for a solution which would enable an admin to make changes to the constants; an admin who does not want to muck around Apex 100%.
thanks,
Odie
Hi,
I use custom labels and not just for boilerplate text. Setup->Create->Custom Labels. You have the advantage that if you do use it for boilerplate rather than hard coding into VisualForce pages, Apex messages etc, then the use of the Translation Workbench will enable language translations to be automatically picked up.
Use in Apex like this: if (max_retries > Integer.valueof(Label.max_retries)) for example.
Hope this helps..
A slight twist on some of the other solutions presented here, I've started using "Custom Settings" (Develop > Custom Settings) to store configurable key/value pairs. (For example: a URL for an environment-specific external webservice endpoint that I don't want to hardcode within APEX code)
I have one Custom Settings object called "Constants" with one text field called "value". Each "data set" name acts as the key, and the "value" field contains the configurable value. One nice thing about this is that the Custom Settings are cached in memory, so you don't have to use SOQL.
I created a utility class with a static method to abstract the retrieval of the constants:
//slimmed down version...
public static String getConstant(String key) {return Constants__c.getValues(key).value__c;}
This seems to meet my needs... but to be honest it still feel like a bit of a hack. Am I missing something?
Comments welcome.
Custom Settings is the way to go! I actually explored it in another post and have since implemented it in a few applications.
Hi, this post is from last year, but I agree, now that we have available the "Custom Setting" object, it is the best way to set constant values and manage them easily.
I've already used them in several projects and they have worked fine.
See the available documentation at: Custom Setting Docs
Regards,
Wilmer
For example, we were recently bit by a bug where a developer did a query like:
SELECT Id FROM RecordType WHERE sObjectType = 'Case' and DeverloperName = :System.Label.Training_Client_Case_Rec_Type
The DevName for the record type was 'Training_Client_Case', and the label was defined with that value in English, but then it was translated into German as 'Training (Kundenvorgang)'. Suddenly the system was searching for a 'Training (Kundenvorgang)' record type for German users and failing because none existed.