DEV Community

Explorer
Explorer

Posted on

How to Update Joget App Environment Variables Programmatically in BeanShell

In Joget DX, App Environment Variables are commonly used to store global configuration values—such as API endpoints, tax rates, batch counter sequences, or feature flags.

While administrators can update these variables manually through Joget App Center, enterprise workflows often need to update environment variables programmatically (for example, incrementing a daily batch sequence counter or updating an OAuth access token).

In this guide, we'll write a short BeanShell script using Joget's EnvironmentVariableDao to fetch and update App Environment Variables dynamically.


How It Works

  1. Obtain App Context: AppUtil.getCurrentAppDefinition() retrieves the active application definition.
  2. Access the DAO Bean: AppUtil.getApplicationContext().getBean("environmentVariableDao") retrieves Joget's internal DAO for environment variables.
  3. Load & Update: environmentVariableDao.loadById(envVarId, appDef) retrieves the target variable instance. Modifying .setValue() and executing environmentVariableDao.update(envVar) persists the updated value immediately.

The Code

Place this BeanShell snippet inside a BeanShell Tool workflow step or a Form Post-Processing Tool:

import org.joget.apps.app.dao.EnvironmentVariableDao;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.model.EnvironmentVariable;
import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.LogUtil;

public void updateAppEnvironmentVariable(String variableId, String newValue) {
    AppDefinition appDef = AppUtil.getCurrentAppDefinition();

    if (appDef != null) {
        // Retrieve Joget's Environment Variable DAO bean
        EnvironmentVariableDao envDao = (EnvironmentVariableDao) AppUtil.getApplicationContext().getBean("environmentVariableDao");

        // Load target environment variable by ID
        EnvironmentVariable envVar = envDao.loadById(variableId, appDef);

        if (envVar != null) {
            LogUtil.info("EnvVar Manager", "Current value for '" + variableId + "': " + envVar.getValue());

            // Update value and save to database
            envVar.setValue(newValue);
            envDao.update(envVar);

            LogUtil.info("EnvVar Manager", "Updated '" + variableId + "' to: " + newValue);
        } else {
            LogUtil.warn("EnvVar Manager", "Environment variable '" + variableId + "' not found in app: " + appDef.getId());
        }
    }
}

// Example 1: Update a batch sequence counter
updateAppEnvironmentVariable("last_batch_sequence", "10045");

// Example 2: Update an API auth token
updateAppEnvironmentVariable("api_access_token", "token_xyz98765");
Enter fullscreen mode Exit fullscreen mode

Example Use Cases

  • Dynamic Sequence Counters: Incrementing custom sequential counters when generating daily batch numbers.
  • System Feature Toggles: Dynamically enabling or disabling form features (e.g. maintenance_mode = true) during scheduled maintenance.
  • Cached API Credentials: Saving refreshed access tokens after executing OAuth authentication flows.

Important Note

Environment variables updated via EnvironmentVariableDao.update() take effect immediately across all subsequent form loads and workflow executions within the app.

Top comments (0)