Using triggers in get operations.

When performing get data operations you can use two types of triggers - AfterSelect and AfterNew. AfterSelect type is available both for the Main entity and collections. AfterNew 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();
    }

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.