Using automated actions on time conditions

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.

Getting ready

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.

How to do it...

To create an automated action with a timed condition on tasks, follow these steps:

  1. In the Settings top menu, select the Technical | Automation | Automated Actions menu item, and press on the Create button.
  2. Fill out the basic information on the Automated Actions form:
    • Rule Name: Deadline Near Notification
    • Related Document Model: Task
    • Under the Conditions tab, for When to Run, select Based on Time Condition
  3. To set the record criteria, on the Filter field text box, just after the Select Records link, set a valid domain expression: [('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.
  4. To set the time condition, on Trigger Date select the field to use, Deadline, and set the Delay After Trigger Date to -1 Days.
    How to do it...
  5. On the Actions tab, under Server actions to run, click on Add an item and pick Send Reminder from the list that should have been created previously. If not, we could now also create the server action to run using the Create button.
    How to do it...
  6. Click on Save to save the automated action. Perform the following steps to try it out:
    1. Go to the Project top menu, Search | Tasks, and set a deadline on some task with a date in the past.
    2. Go to the Settings top menu, to the Technical | Automation | Scheduled Actions menu item, find the Check Action Rules action in the list, open its form view, and press on the Run Manually button, at the top left. This forces timed automated actions to be checked now. Note that this should work on a newly created demo database, but might not work like this in an existing database.
      How to do it...
    3. Again go to the Project top menu and open the same task you previously set a deadline date to. Check the message board below; you should see the notification generated by the server action triggered by our automated action.

How it works...

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.

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset