Adding actions to an entity
In this section, we will be working with the actions
of an entity.
Let's go back to the builder and get to work.
We already have the basic operations to create
, edit
, and delete
tasks. However, it would be better to have a custom workflow that enforces some rules. For example, a task can only be moved to "In progress"
if it is in the status "To do"
. To create the workflow, we will:
✅ Create actions to move the task through the different statuses.
✅ Do not allow modification of the status manually.
✅ Add a column to the grid view.
Create the action “Start work”
👉 Right-click on the node Model > Entities > Tasks > Actions
, a dropdown menu will be shown, click "New Action"
.
👉 Fill in the form with:
Label
: Start workName
: startWork.
👉 In the Preconditions section, you will indicate under which conditions the action can be executed. For the "startWork"
action, the precondition is that the field "Status"
must be "To do"
. This can be indicated with an expression. Select the option "Expression"
for Preconditions, then set up the following expression by clicking on "Add new rule"
and configure it like this:
👉 In the field "Action script"
, add the following script to the body of the function:
record.lock(function(record) {
record.field('status').val('inProgress');
sys.data.save(record);
});
Create some more actions
Now it’s your turn!
In the following table, you will find the settings for the actions required to handle different task statuses. Please repeat the instructions from the previous step to create each action.
Label | Name | Precondition | Action script |
---|---|---|---|
Complete | complete | Status equals to In progress |
|
Archive | archive | Status NOT equals toArchived |
|
Stop work | stopWork | Status equals to In progress |
|
Reopen | reopen | Status equals to Done or Archived |
|
Limit write access to status field
Good, now we have all the actions to manage the workflow! However, there is one problem: anyone can simply change the status field by editing the task. This is not what we want; instead, we want people to follow the actions workflow we have defined. To achieve this, we need to make the Status field read-only:
- Click on the node
Model > Entities > Tasks > Fields > Status
. - Select the
Display options
tab. - For the option
Read only
selectAlways
. - Save changes by clicking on
Apply
.
Adding the actions column to the grid view
Finally, we will add a column to the grid view that shows the actions available for that record:
- Click on the node
Model > Entities > Tasks > Views > All tasks
. - Inside Menu settings, set the option
"Record menu"
to"All"
and set the flag"Display record menu column"
to"active"
. - Save changes by clicking on
"Apply"
.
Awesome! Go ahead and push the changes.
To do that, you need to click the icon next to your username (The hamburger icon). A drop-down menu will be shown, then click "Push changes"
.
🎉
Congratulations! Now you have learned how to create actions to model the behavior of the entity fields and records.
Let’s wrap up this session here. Go to the runtime tab, and we will continue on to the next session Testing actions on runtime