Table methods

Completed

Tables are main elements of finance and operations apps development. Many standard tables are already available in the application. You can also create a new custom table.

Each table has methods, which can be classified as standard methods or custom methods. Standard tables must have a super() call to run the framework logic. This requirement also applies to custom tables because they're derived from the same base type.

You can extend the existing table methods and include code by using the chain of command option. Additionally, you can create table delegates to include code in an existing method. These delegates should be subscribed to add functionality to an existing table method.

Standard table

The following sections explain the various methods that are used in standard tables in the finance and operations apps development platform.

initValue()

The initValue() method is a standard method that's called when a new record is created. You can use this method to address the requirement of initializing some field values while a new record is being created.

Example

You have a custom table named TrainingMaster. The requirement is to initialize the TrainingType field of that table with enum element Online while a new record is inserted into this table.

The coding pattern is as follows:


    public void initValue()
    {
        super();
        this.TrainingType = TrainingType::Online;
    }

find()

The find() method is a custom method that's usually created with all tables. The find() method returns a specific record based on primary key or other unique keys. This method is a static method.

Example

You have a custom table named TrainingParameters. You need to create the find() method that will return one record.

The coding pattern is as follows:

static TrainingParameters find(boolean _forupdate = false)
    {
        TrainingParameters parameter;

        if (_forupdate)
        {
            parameter.selectForUpdate(_forupdate);
        }

        select firstonly parameter
            index Key
            where parameter.Key == 0;

        if (!parameter && !parameter.isTmp())
        {
            Company::createParameter(parameter);
        }

        return parameter;
    }

insert()

The insert() method is a standard method that's called when a new record is inserted into a table. You can use this method in these scenarios:

  • Populate data in a field based on the value of some other field in the table.
  • Run a process while data is inserted in a table.

Example

In the custom table TrainingMaster, you need to populate data into the Price field. The value of this field is a multiplication of the following two fields:

  • NoofDays field from the TrainingMaster table
  • PerDayPrice field from the TrainingParameter table

The coding pattern is as follows:

public void insert()
    {
        this.Price = this.NoofDays * TrainingParameters::find().PerDayPrice;
        super();
    }

modifiedField()

The modifiedField() method is a standard method that's called when a field of a record is modified. You can use this method in these scenarios:

  • Populate data into a field while some other field is modified in the same record.
  • Run a process while some field is modified in a record.

Example

The requirement is to erase the value from the Venue field of the TrainingMaster table, while the TrainingType is Online.

The coding pattern is as follows:

public void modifiedField(FieldId _fieldId)
    {
        super(_fieldId);

        switch (_fieldId)
        {
            case fieldNum(TrainingMaster, TrainingType):
                this.Venue = (this.TrainingType == TrainingType::Online ? "" : this.venue);
                break;
        }
    }

validateField()

The validateField() method is a standard method that's called to validate the data that's entered into a field. You can use this method to perform data validation while data is being entered into the field.

Example

The requirement is to restrict entering data into the venue field of the TrainingMaster table, if the TrainingType is Online.

The coding pattern is as follows:

public boolean validateField(FieldId _fieldIdToCheck)
    {
        boolean ret = super(_fieldIdToCheck);
        
        switch(_fieldIdToCheck)
        {
            case fieldNum(TrainingMaster, Venue):
                if (this.TrainingType == TrainingType::Online && this.Venue)
                {
                    ret = ret && checkFailed("Online training does not require venue");
                }
                break;
        }
        return ret;
    }

validateWrite()

The validateWrite() method is a standard method on tables. This method is called when the Save button is selected or a user attempts to navigate out of a record after inserting a new record or updating an existing one. Override this method if a condition should be validated before you leave the record.

Example

You have a custom form with a table named TrainingMaster. The table has a field named TrainerID that creates a foreign key with the TrainerTable. TrainerTable has a real field Rate, which captures the daily rate of the trainers. When you save the record in the TrainingMaster table, the system should check if Rate is defined for the corresponding trainer.

The coding pattern can be as follows:

public boolean validateWrite()
    {
        boolean ret;
         
        ret = super();

        if(!TrainerTable::find(this.TrainerID).Rate)
        {
            ret = checkFailed("Please enter Trainer's rate");
        }
    
        return ret;
    }

Extension of table methods

Chain of command is an option that helps you enhance the functionality of a table method. This option is applicable for standard methods and custom methods.

In the following example, the requirement is to initialize the Price Rounding rule of the Currency table with 0.01 whenever a new record is created. A table extension class will be created with this code snippet of the initValue() method.

[ExtensionOf(tableStr(Currency))]
final class CurrencyTable_Extension
{
    public void initValue()
    {
        this.RoundOffPrice = 0.01;
        next initValue();
    }

}

The next keyword is calling the parent method from the extended method. If a new custom method is created in the extended table, you don't need to use the next keyword.