Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How to open a form with ir.action.server which executes python code

By
Eric
on 9/4/14, 1:26 PM 6,662 views

Hello, 

  I have a custom module that has a new item in the "More" contextual dropdown menu.  When the item is clicked, it calls an action saved to the ir.action.server.  The action executes python code like it's supposed to.  The python code creates a new record on the sale.order table and is supposed to open the sale.order form with the newly created record.  I have a return statement at the end of the python code which is supposed to open the sale.order form, but for some reason it isn't.  I don't understand why, because the same return statement works on other function calls except this one.  Can anyone help me open the sale.order form after my function has created a new record on the sale.order model?

Here is the code that adds the line to the "More" contextual dropdown:

<record model="ir.values" id="test_more_item">
            <field name="name">New More Item</field>
            <field name="model" eval="'sale.licenses'" />
            <field name="key" >action</field>
            <field name="key2">client_action_multi</field>
            <field name="value" eval="'ir.actions.server,%d'%sale_license_more_item_action" />
            <field name="object" eval="True" />
        </record>

 

This is the action the the "More" item calls.  its from the ir.action.server

<record id="sale_license_more_item_action" model="ir.actions.server">
            <field name="name">Renew Licenses</field>
            <field name="type">ir.actions.server</field>
            <field name="res_model">sale.order</field>
            <field name="model_id" >283</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="state">code</field>
            <field name="code">self.renew_license(cr, uid, context.get('active_ids', []), context=context)</field>
        </record>

 

This is the function that is called from the server action

def renew_license(self, cr, uid, ids, context=None):

     ...(code that creates new sale.order record)

     return {
            'name': 'Sale Order Form',
            'view_type': 'form',
            'view_mode': 'form',
            'view_id': self.pool.get('ir.ui.view').search(cr, uid, [('name','=','sale.order.form')])[0],
            'res_model': 'sale.order',
            'res_id': new_order_id,
            'type': 'ir.actions.act_window',
        }

Normally the return statement would open the sale.order form, but in this case it isn't.  Does anyone know what I am doing wrong with this?

2
Eric
On 9/8/14, 1:26 PM

I finally got the answer right.  Thanks to Maniganda's answer found on this link: 

https://www.odoo.com/forum/help-1/question/call-function-from-the-more-dropdown-list-61330

 

All I needed to do was change this line:

<field name="code">self.renew_license(cr, uid, context.get('active_ids', []), context=context)</field>

 

To this:

<field name="code">action = self.renew_license(cr, uid, context.get('active_ids', []), context=context)</field>

 

Thank you Maniganda!

0

Ben Bernard

--Ben Bernard--
1083
| 4 3 6
Jakarta, Indonesia
--Ben Bernard--
Ben Bernard
On 9/5/14, 2:06 AM

This is my guessing (I never do this). I think the problem is that new sale order, new_order_id, record hasn't been commited. As far as I know odoo commit transaction at the end of request. So when the method return an action, odoo makes the action void because new_order_id is not available yet.

Hello Ben, I tried testing this by using an ID of another sale order, but it still didn't open the form view. Thanks for your reply

Eric
on 9/8/14, 11:37 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/4/14, 1:26 PM
Seen: 6662 times
Last updated: 3/16/15, 8:10 AM