DEV Community

Peter + AI
Peter + AI

Posted on

πŸš€ Uniface Trigger Activation: When One Click Creates a Chain Reaction

This post is based on Uniface Documentation 10.4 and was created with AI assistance.

Hey developer community! πŸ‘‹

Today we're diving into the fascinating world of Uniface trigger activation. If you've ever worked with event-driven applications, you know how complex the behind-the-scenes processes can be. Uniface is no exception – but fortunately, there's a clear logic behind it! 🧠

🎯 What Are Triggers in Uniface?

Uniface applications are event-driven. This means that certain events activate corresponding triggers. These triggers can be activated by various sources:

  • πŸ‘€ User actions (clicks, inputs, etc.)
  • πŸ“ ProcScript statements
  • πŸ”” External events (asynchronous interrupts)

When an event occurs, the corresponding trigger is activated, and the script contained within determines what happens next. Pretty straightforward, right? πŸ€”

πŸ”„ Implicit Trigger Activation

Here's where it gets interesting! Some events happen implicitly – meaning they're automatically triggered without you having to explicitly program them.

A typical example in Form components:
πŸ” loseFocus - Field-level trigger
πŸ”‘ leaveModifiedKey - Occurrence-level trigger
✏️ leaveModified - Occurrence-level trigger

Imagine this: A user modifies a key field and attempts to store the data without actually leaving the field. What happens behind the scenes? 🎭

🌊 Cascading Processing

This is the exciting part! A single user click can trigger a chain reaction. Let's look at what happens when storing (^STORE) in a Form component:

The Domino Effect πŸ€„

  1. πŸ–±οΈ User clicks a button β†’ ^STORE function is activated

  2. βœ… Uniface automatically activates the required validation triggers:

    • validate and validateKey (data validation)
    • loseFocus, leaveModifiedKey, leaveModified (event completion)
  3. πŸ’Ύ Store trigger of the component is activated

  4. ✍️ Write trigger is activated for every modified entity occurrence

  5. πŸ” formatToDbms trigger is activated for each field

  6. πŸ—ƒοΈ Data is passed to the database connector and stored in the database

🧩 The Different Trigger Levels

Uniface organizes triggers in different hierarchy levels:

Level Example Triggers Description
πŸ—οΈ Component store, accept, preActivate, postActivate, receiveMessage, quit Acts on the entire component
πŸ“Š Occurrence write, delete, validate, validateKey, writeUp, deleteUp Acts on record level
🏷️ Field loseFocus, formatToDbms, formatFromDbms, validate, error Acts on individual fields

⚠️ Important Note for Developers

Here's a critical point: If you omit the store statement in the store trigger, the write trigger won't be activated! This means your data won't be saved to the database. 😱

; βœ… Correct - activates write trigger
store

; ❌ Wrong - write trigger won't be activated
; (store statement missing)
Enter fullscreen mode Exit fullscreen mode

πŸ†• Uniface 10 Specific Features

Explicit Trigger Declaration πŸ“

One of the most important changes in Uniface 10 is that triggers must be explicitly declared using the trigger statement:

trigger triggerName
  ; Your trigger code here
  ; Example: validation logic, data processing, etc.
end
Enter fullscreen mode Exit fullscreen mode

This is a significant departure from earlier versions where triggers could be implicitly defined. Every trigger you want to use must be explicitly declared, even if it only contains comments or whitespace.

Default Behavior Clarification πŸ”§

It's important to understand that most triggers have NO default behavior. This means:

  • If you don't define a trigger, nothing happens when the corresponding event occurs
  • Only a few specific triggers have implicit behavior that activates when no explicit trigger is defined
  • This gives developers complete control over application behavior

Inheritance and Override Rules πŸ—οΈ

In Uniface 10, trigger inheritance follows strict rules:

  • Local trigger declaration breaks inheritance - even if the trigger is empty
  • When you declare a trigger locally, it completely overrides any inherited behavior
  • This applies even to triggers that contain only comments or whitespace
; This breaks inheritance even though it's "empty"
trigger validate
  ; Even this comment breaks inheritance!
end

; This also breaks inheritance
trigger validate

end
Enter fullscreen mode Exit fullscreen mode

🎯 The Complete Processing Flow

When a user performs a simple "modify and store" action, Uniface actually processes multiple triggers behind the scenes:

  • loseFocus - field-level trigger
  • leaveModifiedKey - occurrence-level trigger
  • leaveModified - occurrence-level trigger
  • store - component-level trigger
  • write (or writeUp) - occurrence-level trigger for every occurrence that needs updating
  • formatToDbms - field-level trigger for every field stored in encrypted format
  • delete (or deleteUp) - occurrence-level trigger for every removed occurrence

Much of this processing can be handled with single ProcScript instructions like store, write, and delete. If all triggers complete successfully, the user's data is correctly stored in the database.

🎼 Best Practices for Uniface 10 Developers

1. Always Declare Your Triggers Explicitly βœ…

trigger validate
  ; Always use explicit trigger declaration
  if (fieldvalue < 0)
    error "Value must be positive"
  endif
end
Enter fullscreen mode Exit fullscreen mode

2. Understand Inheritance Impact πŸ”„

; Be aware that this breaks inheritance
trigger validate
  ; Your custom validation
  ; Parent validation is NO LONGER called
end
Enter fullscreen mode Exit fullscreen mode

3. Handle Cascading Carefully ⚑

trigger store
  ; Remember: omitting 'store' prevents write triggers
  store  ; This line is CRITICAL
end
Enter fullscreen mode Exit fullscreen mode
  1. Document Your Trigger Dependencies πŸ“š
trigger write
  ; This trigger depends on:
  ; - validate trigger completing successfully
  ; - formatToDbms trigger for field formatting
  ; - store trigger activation
end
Enter fullscreen mode Exit fullscreen mode

πŸ” Debugging Trigger Chains

When debugging complex trigger chains, consider:

  • Enable trace logging to see trigger activation sequence
  • Use breakpoints in critical triggers like validate and write
  • Check for missing explicit declarations - a common source of bugs in Uniface 10
  • Verify inheritance behavior - ensure parent triggers are called when expected

🎼 Summary

Trigger activation in Uniface is like a perfectly orchestrated symphony:

  • A single event can trigger a complex chain of triggers
  • Uniface automatically handles data validation and integrity
  • The hierarchy (Component β†’ Occurrence β†’ Field) ensures structured processing
  • Each trigger has its specific role in the overall process
  • Uniface 10 requires explicit declaration of all triggers
  • Inheritance rules are strict - local declaration always overrides parent behavior

🀝 Conclusion

Uniface's event-driven architecture may seem complex at first glance, but it provides a powerful and flexible foundation for robust applications. The automatic cascading processing ensures that your data remains consistent and validated – even when much more is happening behind the scenes than the user sees! πŸ”

Uniface 10 brings additional structure and control through explicit trigger declarations, giving developers complete control over application behavior while maintaining the powerful cascading capabilities that make Uniface unique.

The beauty of this system lies in its implicit intelligence – developers don't need to manually orchestrate every step of the validation and storage process. Uniface handles the complexity while providing clear extension points through triggers.

Understanding these concepts is crucial for building maintainable, robust Uniface applications. The explicit nature of Uniface 10 triggers might require more initial setup, but it provides better code clarity and prevents many common inheritance-related bugs.

Have you worked with Uniface triggers before? How has your experience been with the transition to Uniface 10's explicit trigger declarations? Share your experiences in the comments! πŸ’¬

Based on Uniface Documentation 10.4 - Trigger Activation (last updated: August 22, 2024)

Top comments (0)