This question has been flagged

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?

Avatar
Discard
Best Answer

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
Avatar
Discard
Author

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?

Best Answer

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.

Avatar
Discard
Author

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