Skip to content

HowToReadonlyStoredField

iraichi edited this page Jun 25, 2015 · 8 revisions

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.

First case: you want to calculate the field value before storing

In this case you have to define a model rule in your Kitto project that fires before store.

Example: Code in Employee.yaml

ModelName: Employee
.......
Rules:
  EmployeeCalcDescription:

Code in Rules.pas

unit Rules;
.....
type
  TEmployeeCalcDescription = class(TKRuleImpl)
  public
    procedure BeforeAddorUpdate(const ARecord: TKRecord); override;
  end;
.....

implementation
......

procedure TEmployeeCalcDescription.BeforeAdd(const ARecord: TKRecord);
begin
  inherited;
  ARecord.FieldByName('Description').Value := 
    ARecord.FieldByName('Surname').AsString + ' ' + ARecord.FieldByName('Name').AsString);
end;

Second case: you want to show the value of the calculated field at screen

In this case you have to write a field rule in your Kitto project that fires at change of Name or Surname.

Example: Code in Employee.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

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;

Clone this wiki locally