When should a developer use a Constant in Appian instead of hardcoding a value?
When should a developer use a Constant in Appian instead of hardcoding a value?
Answer
A constant in Appian stores a value you can reuse across application objects such as interfaces, expression rules, process models and integrations.
You define the value once in a constant and reference it with cons!, so you don’t have to type the same value in several places.
Use a constant when a value is reused, needs to be maintained in one place, or has to be set differently in each environment.
Example: Invoice Approval Application
Take an Appian application where any invoice over $10,000 needs Senior Manager approval.
Approach 1: hardcoding (not recommended)
The developer writes the threshold straight into the expression:
The problem:
Say the business raises the approval threshold from $10,000 to $15,000.
If 10000 is hardcoded in several expression rules, interfaces and process models, the developer has to find and change every one of them.
That’s more maintenance work, and if one occurrence gets missed, different parts of the app apply different rules.
Approach 2: using a constant (recommended)
Create a constant:
- Name:
INV_APPROVAL_THRESHOLD - Type: Integer
- Value:
10000
Then reference it in the expression:
The benefit:
If the threshold changes to $15,000, you update the constant’s value and nothing else.
Every expression that references the constant picks up the new value automatically.
Common uses for constants
Use case | Example |
|---|---|
Business thresholds |
|
Appian groups |
|
Status values |
|
Document folders |
|
Environment-specific settings |
|
Configuration values |
|
Environment-specific constants
Constants also help when a configuration value differs between environments.
For example, an external API endpoint might be different in DEV, TEST and PROD:
Environment | API URL |
|---|---|
DEV | |
TEST | |
PROD |
Instead of hardcoding these URLs in different expressions, create an environment-specific constant such as cons!INV_API_BASE_URL.
When you deploy between environments, you set these values through an import customization file.
Important: the Environment Specific option works for primitive and Email Address constant types. Turn it on only when the value really is different in each environment.
When should you avoid constants?
Not every value belongs in a constant:
- Simple literals: you can hardcode values like
true,false,0or1when their meaning is obvious and they aren’t business settings. - User-entered values: use rule inputs or process variables instead.
- Business data that changes often: store it in a database or record type so you aren’t updating a constant again and again.
- Sensitive credentials: set up secure authentication in Connected Systems instead of putting passwords or API secrets in ordinary constants.
Reference : Appian Official Documentation – Constant Object
Check yourself
Question 1 of 3, on what the answer above covers.
- 1.An Appian application requires Senior Manager approval for invoices above $10,000. The threshold is used in multiple expression rules and process models. The business may change this amount in the future.What is the best approach?
- 2.Your Appian application uses different API URLs in DEV, TEST, and PROD. During deployment, the production URL must be configured without modifying the integration logic.What is the recommended approach?
- 3.An Appian application maintains product prices that change frequently. Business users update these prices through an admin interface, and the latest prices must be available immediately.Where should these values be stored?