Skip to main content

Calculated Fields

It is possible to create a ValueField for which the value is not stored in the database, but rather derived based on other values.

First, you will need to define a value fieldFieldA property of a DataElement expanded to a database column and user interface input element. with FieldType set to CALCULATED_FIELD:

<field name="totalWeight">
<fieldType>CALCULATED_FIELD</fieldType>
<valueField>
<valueFieldType component="" name="Long"/>
</valueField>
</field>

After expanding this, the CrudsClass will contain a method stub to implement the calculation.

StarshipCruds
@Override
public CrudsResult<Long> performCalculateTotalWeight(ParameterContext<StarshipData> dataParameter) {
StarshipData starshipData = dataParameter.getValue();
Long result = null;

// anchor:custom-calculation-totalWeight:start
result = starshipData.getComponents().stream()
.mapToLong(StarshipComponentData::getWeight)
.sum();
// anchor:custom-calculation-totalWeight:end

return CrudsResult.success(result);
}

You can then add this fieldFieldA property of a DataElement expanded to a database column and user interface input element. to a ProjectionProjectionA tailored subset of fields on a DataElement designed to encapsulate data for a specific technical or functional view., or as an info fieldFieldA property of a DataElement expanded to a database column and user interface input element..