Problem Statement
| English |
|---|
| For any assignment form that is running on older app versions, any use of ID Generator Field/Tool would be picking the next running number from the app version it started on. |
| App Version | Published | Environment Variable "counter" Value |
|---|---|---|
| 1 | 10 | |
| 2 | 10 | |
| 3 | Yes | 10 |
...
In the form where the ID Generator Field is in, modify the store binder, and set it to Bean Shell Form Binder.
Modify line 17 to 27 to suit your app.
| Code Block | ||||
|---|---|---|---|---|
| ||||
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.dao.EnvironmentVariableDao;
import org.joget.apps.app.model.EnvironmentVariable;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.DecimalFormat;
import org.joget.commons.util.StringUtil;
import org.joget.apps.form.model.FormStoreBinder;
import org.joget.plugin.base.PluginManager;
//Your App ID here
String appId = "SampleApp";
//Environment Variable ID here
String envId = "PaymentRunningNumber";
//ID generator field ID here
String genFieldId = "field3";
//ID generator value format
String format = "RECEIPT/#date.MMyy#/??????";
public FormRowSet store(Element element, FormRowSet rows, FormData formData) {
FormRow row = rows.get(0);
String result = incrementEnvVar(appId,envId);
row.setProperty(genFieldId, result);
//Reuse Workflow Form Binder to store data
PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
FormStoreBinder binder = (FormStoreBinder) pluginManager.getPlugin("org.joget.apps.form.lib.WorkflowFormBinder");
binder.store(element, rows, formData);
return rows;
}
public String incrementEnvVar(String appId, String envId) {
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
/* Set null to always get latest version */
//AppDefinition appDef = appService.getAppDefinition(appId,null);
/* Set with publishedVersion to always get published version */
Long publishedVersion = appService.getPublishedVersion(appId);
AppDefinition appDef = appService.getAppDefinition(appId,publishedVersion.toString());
EnvironmentVariableDao environmentVariableDao = (EnvironmentVariableDao) AppUtil.getApplicationContext().getBean("environmentVariableDao");
EnvironmentVariable env = environmentVariableDao.loadById(envId, appDef);
/* For testing only to check retrieved env-var */
// System.out.println("This is env-var value --> " + env.getValue());
Integer count = null;
if (publishedVersion.compareTo(Long.parseLong("#appDef.version#")) == 0) {
count = Integer.parseInt(env.getValue());
} else {
count = environmentVariableDao.getIncreasedCounter(envId, null, appDef);
}
String value = format;
Matcher m = Pattern.compile("(\\?+)").matcher(format);
if (m.find()) {
String pattern = m.group(1);
String formatter = pattern.replaceAll("\\?", "0");
pattern = pattern.replaceAll("\\?", "\\\\?");
DecimalFormat myFormatter = new DecimalFormat(formatter);
String runningNumber = myFormatter.format(count);
value = value.replaceAll(pattern, StringUtil.escapeRegex(runningNumber));
}
return value;
}
return store(element, rows, formData); |
...