Automated actions can be used to automatically trigger actions based on time conditions. We can use them to automatically perform some operation on records that meet certain criteria and reach time condition.
As an example, we can trigger a reminder notification for Project tasks one day before their deadline, if they have any. Let's see how this can be done.
To follow this recipe, we will need to have both the Project Management app and the Automated Action Rules addon already installed, and have the Developer Mode activated. We will also need the server action created in the Using Python code server actions recipe.
To create an automated action with a timed condition on tasks, follow these steps:
[('date_deadline', '!=', False), ('stage_id.fold', '=', False)]
. When changing to another field, information on the number of records meeting the criteria is updated, and the Select Records link changes to Change Selection. By clicking on it we can check the records list of the records meeting the domain expression.Automated actions act on a Model, and can be triggered either by events or by time conditions. The first steps are to set the Model and When to Run values.
Both methods can use a filter to narrow down the records eligible to perform the action on. We can use a domain expression for this. You can find details on writing domain expressions in Chapter 8, Backend Views. Alternatively, you can create and save a filter on project tasks, using the user interface features, and then copy here the automatically generated domain expression, selecting it from the Set selection based on a search filter list.
The domain expression we used selects all the records with a non-empty Deadline date, in a stage where the Fold flag is not checked. Stages without the Fold flag are considered to be working in progress. This way, we avoid triggering notifications on tasks that are in the Done, Canceled, or Closed stages.
Then we should define the time condition: the date field to use and when in time the action should be triggered. The time period can be in minutes, hours, days, or months, and number of periods can be positive, for time after the date, or negative, for time before the date. When using a time period in days, we can provide a Resource Calendar defining the working days, and the day count will use it.
These actions are checked by the Check Action Rules scheduled job. Note that by default it is run every four hours. This is appropriate for actions that work on the days or months scale, but if you need actions that work in the hour or minute timescales, you need to change the running interval to a smaller value.
Actions will be triggered for records that meet all the criteria and whose triggering date condition (field date plus the interval) is after the last action execution. This is so as to avoid repeatedly triggering the same action. And this is why manually running the preceding action will work in a database where the scheduled action was not yet triggered, but might not work immediately in a database where it was already run by the scheduler.
Once an automated action is triggered, the Actions tab tells you what should happen. This can be a list of server actions, doing things such as changing values on the record, posting notifications, sending out e-mails, and so on.
Additionally, two special operations are available. Set Responsible expects the target Model to have a
user_id
field and sets its value when the action is triggered. Add Followers gives a list of partners to be added to the record followers.
These types of automated actions are triggered once a certain time condition is reached. This is not the same as regularly repeating some action while some condition is still true. For example, an automated action would not be capable of posting a reminder every day after the deadline has exceeded.
This type of action can instead be performed by Scheduled Actions, stored in the ir.cron
model. However, scheduled actions do not support server actions; they can only call an existing method of a model object. So, to implement a custom action, we need to write an addon module adding the underlying Python method.
For reference, the technical name for the Automated Actions Model is base.action.rule
.