Hi,
I need your help, I’m working on something that I had thought was simple but it’s proving rather complex for me.
Scenario:
I have three entities, KPI
, Achievement
& Tasks
.
Relationships
-
The Achievement is a child entity of the KPI. The KPI has a total achievement field which is an aggregation of several achievements.
-
One KPI can be assigned to several Tasks.
-
A Task has a target assigned to it, and several Achievements can be linked to that one Task.
-
The Achievements in 3 Above, should in turn be recorded on the KPI as well since the Task is linked to a KPI.
The Problem
- Is it possible to automatically update the KPI cumulative achievement field when an Achievement is added regardless of where it is added from, whether on the KPI edit screen or on the Task Edit screen?
I’ve tried the code below on the KPI Edit Screen but nothing. Though setting the KPI Achievements on pre-commit works.
@Override
public void init(Map<String, Object> params) {
updateAchievements();
super.init(params);
}
private void updateAchievements() {
achievementsDs.addCollectionChangeListener(e -> {
if (e.getItems().size() !=0){
setKPIAchievements();
}
});
}
private void setKPIAchievements() {
Double kpiAchievement = 0.0;
log.error("starting task achievement code");
for (Achievement a : getItem().getAchievement()) {
totalAchievement = nullToZero(a.getAchievement());
kpiAchievement = kpiAchievement + totalAchievement;
}
getItem().setTotalAchievement(nullToZero(kpiAchievement));
}
Double nullToZero(Double d) {
if (d == null) {
return 0.0;
} else {
return d;
}
}
I would want an option which uses the **Entity Listeners**
if possible.