How to calculate a readonly stored field
suppose you have a model Employee with field Name, Surname, Description. The user uploads Name and Surname and the program must fill Description with Name + Surname.
You have to define a rule.
Case you want to calculate the field value before storing
You have to define a model rule in your Kittox project that fires before store.
Example: Code in Employee.yaml
yaml
ModelName: Employee
.......
Rules:
EmployeeCalcDescription:Code in Rules.pas
yaml
unit Rules;
.....
type
TEmployeeCalcDescription = class(TKRuleImpl)
public
procedure BeforeAddorUpdate(const ARecord: TKRecord); override;
end;
.....
implementation
......
procedure TEmployeeCalcDescription.BeforeAddorUpdate(const ARecord: TKRecord);
begin
inherited;
ARecord.FieldByName('Description').Value :=
ARecord.FieldByName('Surname').AsString + ' ' +
ARecord.FieldByName('Name').AsString);
end;Case: you want to show the value of the calculated field at screen
You have to write a field rule in your Kittox project that fires at change of Name or Surname.
Example: Code in Employee.yaml
yaml
ModelName: Employee
PhysicalName: EMPLOYEE
Fields:
.....
Description: String(200) not null
PhysicalName: DX
IsReadOnly: True
DisplayWidth: 39
Surname: String(50) not null
PhysicalName: SURNAME
IsVisible: False
IsReadOnly: True
Rules:
EmployeeCalcDescription:
Name: String(50) not null
PhysicalName: NAME
IsVisible: False
IsReadOnly: True
Rules:
EmployeeCalcDescription:Code in Rules.pas
yaml
unit Rules;
.....
type
TEmployeeCalcDescription = class(TKRuleImpl)
public
procedure AfterFieldChange(const AField: TKField;
const AOldValue, ANewValue: Variant); override;
end;
.....
implementation
......
procedure TEmployeeCalcDescription.AfterFieldChange(
const AField: TKField; const AOldValue, ANewValue: Variant);
begin
inherited;
AField.ParentRecord.FieldByName('Description').Value :=
AField.ParentRecord.FieldByName('Surname').AsString + ' ' +
AField.ParentRecord.FieldByName('Name').AsString);
end;