Comment 6 for bug 628767

Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote :

Hello,
This bug is invalid because what you want to achieve is perfectly doable. The part you are missing is the fact that the dynamic definition of an ir.actions.act_window may contain an extra 'res_id' key, which combined with view_mode="form" will directly open the form view of the given record. As of v7.0 the res_id column is explicitly defined on ir.actions.act_window. Before v7.0 this key was only supported in dynamic action definitions, such as those returned by a method call.

Based on the above, and assuming you have a many2one field 'opportunity_id' on the sale.order model, there are many different ways to directly open the form view of the related opportunity from the sale.order. Here are a few options:

1. The simplest way does not even use an action and does not need the 'res_id' trick at all. You simply add the opportunity_id field in the form view of sale.order, and make sure to pass a special context to choose the "Opportunity" form view instead of the default "Lead" form. This is as simple as adding the following field to the sale.order form view:
  <field name="opportunity_id" context="{'form_view_ref': 'crm.crm_case_form_view_oppor'}"/>
Users can now open the form view of the opportunity from the sale.order directly.

2. As the 'res_id' value of the action will be dynamic, we can't simply create a static 'act_window' action in the database. But we can instead add a new method to sale.order that will return the appropriate action. An example method would be:
    def open_opportunity(self, cr, uid, ids, context=None):
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'crm.lead',
            'res_id': self.browse(cr, uid, ids[0], context).opportunity_id.id,
            'view_mode': 'form',
            'view_type': 'form',
            'view_id': self.pool.get('ir.model.data').get_object(cr, uid, 'crm','crm_case_form_view_oppor').id,
        }
And can be executed by adding a button in the form view of the sale.order, as follows:
   <button type="object" name="open_opportunity" string="Open Opportunity"/>
This achieves the same result.

3. If you need the action to be present in the sidebar, you can't directly an <act_window> that will call a python method, so you will need to go through an ir.actions.server that will accomplish the same thing. You can make the server action call the 'open_opportunity' method from example 2 (or move the code of the method in the server action altogether).
The XML definition for the server action and the 'ir.values' entry to bind it to the sidebar could look like this:

        <!-- Define the server action that returns the special action definition -->
        <record model="ir.actions.server" id="open_related_opportunity">
            <field name="type">ir.actions.server</field>
            <field name="name">Related Opportunity</field>
            <field name="state">code</field>
            <field name="model_id" ref="sale.model_sale_order"/>
            <field name="code">
# Set the value of the `action` variable to return a custom action to
# be executed by the client. 'object' is initialized based on the 'active_id' in the context
action = object.open_opportunity() # based on method in example 2
</field>
        </record>

        <!-- Bind the server action as a sidebar button, as <act_window/> will
             only do it for ir.actions.act_window actions, not server ones. -->
        <record model="ir.values" id="open_related_opportunity_sidebar_binding">
            <field name="name">Related Opportunity</field>
            <field name="model">sale.order</field>
            <field name="key2">client_action_multi</field>
            <field name="value" eval="'ir.actions.server,%d' % open_related_opportunity"/>
        </record>

Examples 2 and 3 are a bit complex because the 'res_id' value needs to be computed at runtime, so a static <act_window> definition as in the bug description would not work (and this field is not evaluated automatically like `domain`). Nevertheless the mechanisms used are pretty straightforward once you understand them.
Have a look at the source code of official addons to see more examples of ir.actions.server and ir.values use.