Type-Safe Constants
Access picklist values and API names with autocomplete support. No more typos in string literals.
A structured, memory-efficient approach for managing constants in Salesforce with singleton pattern and lazy initialization
Stop using hard-coded string literals scattered throughout your code. Apex Consts provides a centralized, type-safe way to manage constants:
Account acc = new Account(
Name = 'My Account',
Type = 'Prospect', // Easy to mistype
Rating = 'Hot' // No autocomplete
);
if (acc.Type == 'Propsect') { // Typo! Runtime error
// ...
}Account acc = new Account(
Name = 'My Account',
Type = Consts.ACCOUNT.TYPE.PROSPECT, // Type-safe
Rating = Consts.ACCOUNT.RATING.HOT // Autocomplete
);
if (acc.Type == Consts.ACCOUNT.TYPE.PROSPECT) { // Compile-time safety
// ...
}// Using Account constants
Account acc = new Account(
Name = 'Acme Corp',
Type = Consts.ACCOUNT.TYPE.PROSPECT,
Rating = Consts.ACCOUNT.RATING.HOT
);
insert acc;
// Using Contact constants
Contact con = new Contact(
FirstName = 'John',
LastName = 'Doe',
LeadSource = Consts.CONTACT.LEAD_SOURCE.WEB
);
insert con;
// Using Opportunity constants
Opportunity opp = new Opportunity(
Name = 'Big Deal',
StageName = Consts.OPPORTUNITY.STAGE_NAME.PROSPECTING,
Type = Consts.OPPORTUNITY.TYPE.NEW_CUSTOMER,
CloseDate = Date.today().addDays(30)
);
insert opp;Apex Consts uses a singleton pattern with lazy initialization:
// First access creates instance
String type = Consts.ACCOUNT.TYPE.PROSPECT; // AccountConsts created
// Later accesses reuse instance
String rating = Consts.ACCOUNT.RATING.HOT; // Reuses same AccountConsts
// Other classes aren't created unless accessed
// ContactConsts not created yet - no memory usedThis approach minimizes heap usage and improves performance.
Apex Consts is part of Apex Fluently, a suite of production-ready Salesforce libraries by Beyond the Cloud.
Ready to eliminate hard-coded strings? Get started →