Working with actions.

Welcome back to the builder. In this section, we are going to be working with the actions of an entity. So let’s 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 do the following things:

  • Create actions to move the issue through the different statuses.
  • Do not allow modification of the status manually.

Let’s create a new action:

  1. Right-click on the node Model > Entities > Tasks > Actions, a dropdown menu will be shown, click New Action.
  2. Fill in the form with:
    • Label: Start work
    • Name: startWork.
  3. In Preconditions you will indicate in which cases the action can be executed. For the action startWork, the precondition is that the field Status must be To do. This can be indicated with an expression, so select the option Expression for Preconditions. Then you will need to set up the following expression by clicking on Add new rule and configure it like this:

    Action precondition

  4. 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);
   });

Great I hope you understood what we did. Now it’s your turn. Add the following actions:

Label Name Precondition Action script
Complete complete

Status equals to In progress

record.lock(function(record) {
   record.field('status').val('done');
   sys.data.save(record);
});
Archive archive

Status NOT equals to Archived

record.lock(function(record) {
   record.field('status').val('archived');
   sys.data.save(record);
});
Stop work stopWork

Status equals to In progress

record.lock(function(record) {
   record.field('status').val('toDo');
   sys.data.save(record);
});
Reopen reopen

Status equals to Done or Archived

record.lock(function(record) {
   record.field('status').val('toDo');
   sys.data.save(record);
});

Good, now we have all the actions to manage the workflow! However, there is one problem: anyone can just change the status field by simply editing the task. This is not what we want; instead, we want to enforce people to follow the workflow we defined.

To prevent people from changing the status in an invalid way, what we will do is make it read-only:

  1. Click on the node Model > Entities > Tasks > Fields > Status.
  2. Select the tab Display options.
  3. For the option Read only select Always.
  4. Save changes by clicking on Apply.

Display options

To keep it simple, we just made the field read-only. However the correct way to do it is to remove permissions to change that field, which will be enforced at the API level and not just he UI. We will see that later.

Finally, we will add a column to the grid view that shows the actions available for that record. Follow these steps:

  1. Click on the node Model > Entities > Tasks > Views > All tasks.
  2. Inside List settings, set the option Show actions to All and set the flag Show actions column to active.
  3. Save changes by clicking on Apply.

Grid view options

Awesome! Go ahead and push the changes. Remember, to do that, you need to click the icon next to your username (The hamburger icon). A drop-down menu will be shown and then click Push changes.

Let’s wrap this session here. Go to the runtime tab and we will continue on to the next session Testing Actions on runtime.

Back to top