Thursday, July 19, 2012

Trigger-like Automated Actions

OpenERP's Automated Actions is a module usually associated with the CRM modules, allowing to automate sales process steps, without the need to write Python code.

Each automated action rule operates on an OpenERP model, any model. It's  behaviour is defined through three tabs:
  • Conditions tell when the rule is fired.
  • Actions tell what operations should be done, like setting a specific user as Responsible person, or running a Server Action.
  • E-mail actions allows to define e-mail messages to be sent .

Conditions can be activated at scheduled moments. This is the sole purpose of the "Conditions on Time" section. The scheduler action evaluates these conditions to decide what rules to trigger. This allows you to configure actions like "send an e-mail reminder two days after last action date".


The remaining conditions are evaluated immediately when a record is saved. This allows you to configure actions like "send an e-mail when state is changed to In Progress".


While you get the feeling of having a lot of potential to easily customize a customer's very specific business rules, actually the module reveals to fall short on the expectations.
For instance, it's not possible to create conditions for:
  • When a new Issue is created, sending an e-mail informing the new Issue;
  • When the Responsible is changed, sending an e-mail informing the new responsible person.
Also, the e-mail actions definition is very confusing and limited. The e-mail Subject line is not configurable, and the message body can only use a small list of keyword to include data from the document.

The reis_base_action_rule_triggers module overcomes these limitations and unlocks a lot of the power of the standard module. This is done through two simple additions:
  • Conditions can be defined by an "Evaluated expression", that can work on record's old and new values.
  • Actions can send an e-mail using v6.1 Email templates, a lot more powerful than the "e-mail actions" options, making them obsolete.
In the condition expressions some variables are available (inspiration came from Oracle's database triggers):
  • inserting, creating: boolean indicating it's a new record is being created;
  • updating, writing: boolean indicating it's an existing record being changed;
  • new: a dictionary with all the values after the record is changed;
  • old: a dictionary with all the values before the record was changed;
  • changed: a dictionary with only the values that actually changed, even if rewritten (empty if not updating);
  • obj: browseable record object, allowing dot notation, with the new/changed record.
Here are some real examples used in a production environment for the Project Issue module:


New issue is created, or is reactivated:
inserting or changed.get('state') == 'draft'


Issue is closed or cancelled:
changed.get('state') in ('done', 'cancel')


Issue's Responsible changed:
old.get('user_id') and new.get('user_id')
and old.get('user_id') != new.get('user_id') 
and not new.get('date_open')  #date_open is written only by Open issue

I hope this turn out useful to more people, and would love to hear about your experience with this module.

5 comments:

  1. Hi, I am trying to get the module to work but it's not sending any mail. I can send emails otherwise, like with the sale auto reply. Should there be a server action? I am obviously missing something. I would be grateful I you could steer me in the right direction.

    Regards,
    A

    ReplyDelete
  2. Some suggestions:
    1) Make sure you're using the "E-mail template" in the "Actions" tab instead of the "Email" tab. And check the "Send immediately" box.
    2) Check for bad emails in Settings » Configuration » Email » Messages. Maybe the mail message fails because of missing required fields, such as "To".
    You may also find useful this SO answer: http://stackoverflow.com/a/13305047/959570

    ReplyDelete
  3. I have the same settings as you in SO, can preview template with correct parameters and send email from issues communication. Any other ideas? I presume that by just filling this expression:
    inserting or changed.get('state') == 'draft'

    The system will then send an auto_reply email to 'To' email recipiant upon creation of an issue?

    ReplyDelete
  4. There should be a "Debug Evaluated expression" checkbox under the "Evaluated Expression" text area. Enable it and restart your server with the --debug option. You should see more details in the server log on what's going on.

    ReplyDelete
  5. This module is obsolete now, is it? Because its not working under openERP 7.x.

    ReplyDelete