You can use the server action for this purpose. You can create a server action in OpenERP by gaining access to the settings "Technical" "Actions" "Server actions or creating XML in your module.
I will give an example of the server action that I use to send an email to the user when the object reaches a certain status in the module that I am developing:
<?xml version="1.0" encoding="UTF-8"?> <openerp> <data> <record id="ir_actions_server_send_email_when_closed_nconf" model="ir.actions.server"> <field name="name">Auto-email when request is closed, not confirmed</field> <field name="model_id" ref="model_generic_request"/> <field name="state">email</field> <field name="type">ir.actions.server</field> <field name="condition">True</field> <field name="email">object.requestor.email</field> <field name="subject">Your request object.name has been closed (not confirmed)</field> <field name="message"><![CDATA[ THIS IS AN AUTOMATED EMAIL. DO NOT REPLY. Hello, We are here to inform you that the request [[object.name]] you submitted on [[object.request_date]] with the following data: | Request - Details |========================= | Number: [[object.id]] |========================= | Responsible Person: [[object.responsible_name.name]] | Request description: [[object.request_description]] | Stating reasons: [[object.stating_reasons]] |========================= | Notes: [[object.notes]] Has not been confirmed and is closed. If you have any question, do not hesitate to contact your supervisor. Thank you!]]> </field> </record> </data> </openerp>
This action is called from a workflow. In your case, you can call it when your form is saved (state = draft, maybe?).
Thus, you must add a call to the server action in determining the activity of the workflow:
<record model="workflow.activity" id="act_closed_nconf"> <field name="wkf_id" ref="wkf_request" /> <field name="name">request_closed_nconf</field> <field name="action_id" ref="ir_actions_server_send_email_when_closed_nconf"/> <field name="kind">function</field> <field name="action">close_nconf_request()</field> <field name="flow_stop">True</field> </record>
Hope this helps!
------ Small editing of a more extended answer -----
Well, I will try to make a short almost functional example.
In your python file, if this is not the case, you need to add some states to work the workflow.
class whatever(osv.osv): _name='whatever' _description='whatever' _columns={ 'name': fields.char('whatever', size=64, required=True), 'state': fields.selection([('draft','Draft'), ('sent','Sent'), ('closed','Closed'), ], 'Status', readonly=True, track_visibility='onchange', ), (... some other fields in here...) } _defaults={ 'state': 'draft', }
Then you need a definition of the workflow that will work on your object, this will be the contents of your xml:
<?xml version="1.0"?> <openerp> <data> <record model="workflow" id="wkf_whatever"> <field name="name">whatever.wkf</field> <field name="osv">whatever</field> <field name="on_create">True</field> </record> <record model="workflow.activity" id="act_draft"> <field name="wkf_id" ref="wkf_whatever" /> <field name="flow_start">True</field> <field name="name">draft</field> <field name="action_id" ref="send_automatic_email"/> <field name="kind">function</field> <field name="action">draft()</field> </record> <record model="workflow.activity" id="act_send"> <field name="wkf_id" ref="wkf_whatever" /> <field name="name">send</field> <field name="kind">function</field> <field name="action">send()</field> </record> <record model="workflow.activity" id="act_close"> <field name="wkf_id" ref="wkf_whatever" /> <field name="flow_stop">True</field> <field name="name">close</field> <field name="kind">function</field> <field name="action">close()</field> </record> <record model="workflow.transition" id="whatever_t1"> <field name="act_from" ref="act_draft" /> <field name="act_to" ref="act_send" /> <field name="signal">draft</field> </record> <record model="workflow.transition" id="whatever_t2"> <field name="act_from" ref="act_send" /> <field name="act_to" ref="act_close" /> <field name="signal">close</field> </record> </data> </openerp>
The string <field name="action_id" ref="send_automatic_email"/> in the activity declaration triggers a server action with the identifier "send_automatic_email"
And your action server:
<?xml version="1.0" encoding="UTF-8"?> <openerp> <data> <record id="send_automatic_email" model="ir.actions.server"> <field name="name">Send automatic email</field> <field name="model_id" ref="model_whatever"/> <field name="state">email</field> <field name="type">ir.actions.server</field> <field name="condition">True</field> <field name="email">object.requestor.email</field> <field name="subject">Your whatever: object.name has been created</field> <field name="message"><![CDATA[ THIS IS AN AUTOMATED EMAIL. DO NOT REPLY. Hello, bla bla bla bla In here you will write whatever you want, and can access to data stored in your database with, for example [[object.name]] to access the field "name" </field> </record> </data> </openerp>
With these three files (and some changes in it!) You can do what you want.
Do not forget that you need to restart the OpenERP server (in order to recompile your changes in the python files) and update the module for loading XML files!
Good luck
- Almost forgot!
In your xml file, you need to add these buttons in the form to trigger workflow actions:
<header> <button name="send" class="oe_highlight" string="Send" type="workflow" states="draft"/> <button name="close" class="oe_highlight" string="Close" type="workflow" states="sent"/> <field name="state" widget="statusbar" statusbar_visible="draft,sent,closed" /> </header>