OnUpdate lets you run EAL (Executable Action Language) each time an existing object is about to be saved, for example to maintain a change timestamp.
When OnUpdate runs
OnUpdate is a framework-recognized method name. MDriven calls it just before an object is saved to the database.
Use it for changes that must be made on every save operation, such as recording when an object was last changed. For object-event behavior in general, see Documentation:Acting on object changes.
Requirements
The method must meet these requirements:
| Requirement | Why it matters |
|---|---|
Method name: OnUpdate
|
The name is hard-coded and recognized by the framework. |
IsQuery is set to false
|
A query method is OCL and side-effect free. OnUpdate changes model objects, so it must be an EAL method. |
| Do not create new objects in the method | A new object created from OnUpdate will be left unsaved. |
Add a change timestamp
To set ChangeTime whenever an existing object is saved:
- Add an
OnUpdatemethod to the class. - Set the method's
IsQueryproperty tofalse. - Enter the following EAL in the method body:
if self.existing then -- This avoids touching the object if it has been deleted
self.ChangeTime:=DateTime.Now;
false
else
false
endifIn this example, self is the object being saved. self.existing checks that the object still exists, so the method does not update an object that has been deleted. DateTime.Now supplies the current date and time, which is assigned to ChangeTime. The expression returns false in both branches.
For example, if a user changes an existing customer's address and saves it, the customer's ChangeTime is set immediately before that customer is saved.
What not to use OnUpdate for
Do not create related log, archive, or other new objects in OnUpdate. Those newly created objects will not be saved.
If you need to decide whether an object has changed since its last save, use isDirty. If you need initialization for a new object, use OnCreate instead. Deletion behavior belongs in OnDelete, and state-machine audit or logging behavior can be handled with OnStateChange.
