Odoo Help

0

Another onchange "not changing" problem...

By
Christian Ruppenthal
on 9/25/13, 10:42 AM 2,111 views

Hello helpers!

I'm working on an extension module for CRM to use with cellphone plan sales and I ran into a problem. Basically what I need is to store date and time on some custom fields on the cdm.lead model. Those fields should record the date and time when an opportunity changes stage to three stages I want to monitor.

Here's what I have now:

**crm_lead.py (on my custom module)**
class crm_lead(orm.Model):
    _inherit = "crm.lead"

    def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
        if not stage_id:
            return {'value':{}}
        stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context)
        if not stage.on_change:
            return {'value':{}}
        return {'value':{'probability': stage.probability}}
        for record in self.browse(cr, uid, ids):
                record.data_mod_est = fields.datetime.now()
                if stage.name == "Envio":
                        record.data_envio = fields.datetime.now()
                elif stage.name == "Cadastro":
                        record.data_cadastro = fields.datetime.now()
                elif stage.name == "Ativacao":
                        record.data_ativacao = fields.datetime.now()
.
.
.
    _columns = {
        'data_envio': fields.datetime('Data de envio'),
        'data_cadastro': fields.datetime('Data de cadastro'),
        'data_ativacao': fields.datetime('Data de ativacao')
    }

And the view...

    **crm_opportunity_view.xml**
.
.
.
                   <!-- CRM Opportunity Form View -->
                    <record model="ir.ui.view" id="l10n_br_crm_case_form_view_oppor1">
                            <field name="name">l10n_br_crm.opportunities1</field>
                            <field name="model">crm.lead</field>
                            <field name="inherit_id" ref="crm.crm_case_form_view_oppor" />
                            <field name="arch" type="xml">
                                    <field name="stage_id" position="replace">
                                            <field name="stage_id" widget="statusbar" clickable="True" on_change="onchange_stage_id(stage_id)"/>
                                    </field>
                                    <field name="categ_ids" position="after">
                                            <field name="data_envio" />
                                            <field name="data_cadastro" />
                                            <field name="data_ativacao" />
                                    </field>
                            </field>
                    </record>
.
.
.

My problem is that when stages change, the fields are not updated. Can someone shed some light on my code?

1

Andreas Brueckl

--Andreas Brueckl--
5121
| 7 8 7
Vienna, Austria
--Andreas Brueckl--
OpenERP Consulting and Development
Andreas Brueckl
On 9/25/13, 10:53 AM

Your methods returns before it comes to the paragraph

for record in self.browse(cr, uid, ids):

Try this:

def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
    if not stage_id:
        return {'value':{}}
    stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context)

    res =  {'value':{'probability': stage.probability}}
    now = fields.datetime.now()
    if stage.name == "Envio":
        res['value']['data_envio'] = now
    elif stage.name == "Cadastro":
        res['value']['data_cadastro'] = now
    elif stage.name == "Ativacao":
        res['value']['data_cadastro'] = now

    return res

I'm a little embarrassed about the return statement, that was pretty obvious... But still, I made the modifications you suggested and the fields are still not being updated. Any other clue?

Christian Ruppenthal
on 9/25/13, 11:21 AM
0

Grover Menacho

--Grover Menacho--
2543
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 9/25/13, 11:09 AM

It's because you are returning data before checking stage name:

def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
        if not stage_id:
            return {'value':{}}
        stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context)
        if not stage.on_change:
            return {'value':{}}
        return {'value':{'probability': stage.probability}} <------ ERROR!
        for record in self.browse(cr, uid, ids):
                record.data_mod_est = fields.datetime.now()
                if stage.name == "Envio":
                        record.data_envio = fields.datetime.now()
                elif stage.name == "Cadastro":
                        record.data_cadastro = fields.datetime.now()
                elif stage.name == "Ativacao":
                        record.data_ativacao = fields.datetime.now()

If you are returning in that place then the rest of the code is not going to be executed.

Thanks for the reply! I modified the code according to Andreas suggestion but fields are still not being updated. Is there anything else that might be wrong? Maybe the browse is not returning the right object...

Christian Ruppenthal
on 9/25/13, 11:27 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)

Stats

Asked: 9/25/13, 10:42 AM
Seen: 2111 times
Last updated: 3/16/15, 8:10 AM