Skip to main content

Introducing Formengine - The New Formbuilder, try for FREE formengine.io.

Using triggers in get operations

When performing get data operations you can use three types of triggers - AfterSelect, AfterNew and AfterCopy. AfterSelect type is available both for the Main entity and collections. AfterNew and AfterCopy works for the main entity only.

These triggers are used to do the following:

  • Changing MainEntity properties and collections. Use AfterSelect, set for the main entity. For example:

    public TriggerResult AfterSelect(EntityModel model, List<dynamic> entities,
    TriggerExecutionContext context, dynamic options)
    {
    foreach (var entity in entities)
    {
    // main entity change
    entity.c = entity.a + entity.b;
    // collection change
    if (entity.collectionName != null)
    {
    foreach (var collectionItem in entity.collectionName)
    {
    collectionItem.c = collectionItem.a + collectionItem.b;
    }
    }
    }
    return TriggerResult.Success();
    }
  • Changing collection properties. Let's say we want to change data displayed in the paging grid. Use AfterSelect bound to the collection which is displayed in the grid.

    public async Task<TriggerResult> AfterSelect(EntityModel model, List<dynamic> entities,
    TriggerExecutionContext context, dynamic options)
    {
    var validationResult = new TriggerValidationResult();

    foreach (var collectionItem in entities)
    {
    collectionItem.c = collectionItem.a + collectionItem.b;
    }

    return TriggerResult.Success();
    }
  • Initialization of properties with values by default when opening new item form (without primary key). Use AfterNew, set for the main entity.

    public TriggerResult AfterNew(EntityModel model, List<dynamic> entities,
    TriggerExecutionContext context, dynamic options)
    {
    foreach (var entity in entities)
    {
    // main entity init
    entity.a = 10; // Some default value
    entity.b = 20; // Some default value
    }
    return TriggerResult.Success();
    }
  • Changing data when copying entities. Use AfterCopy trigger. You can change copied entity in it before it is displayed in the form. Its code will be similar to AfterNew code above.

Note the following features when working with get data triggers:

  • It is useless to apply return TriggerResult.ValidationFailed(...). Such a case cannot be processed.
  • In case of error return from trigger use whether return TriggerResult.Terminate("Error message"); or return TriggerResult.Result("Some error message", terminate: true);.
  • When creating a new item in the edit form, AfterNew is activated first, AfterSelect is next, both set for the main entity.