function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
odie123odie123 

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

WilmerWilmer

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

odie123odie123

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

srisomsrisom

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..

 

CaptainObviousCaptainObvious
How about creating a custom Constants object? Your admins could access/modify the constants through the UI, and your code could read the 'constants' from the object.
cory mcory m

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.

CaptainObviousCaptainObvious

Custom Settings is the way to go! I actually explored it in another post and have since implemented it in a few applications. 

WilmerWilmer

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

Lawrence-AccentureLawrence-Accenture
Although this is an old post, I thought I'd comment.... I'd advise against using custom labels for code constants because the label can be translated (on purpose or by accident), so the value you thought was constant, will now be different for different users depending on their language. Obviously, you should use custom labels for actual labels -- anything that will be displayed to the user, but not for 'constant values' that you use in your code.

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.