Nabil Taleb
Nabil Taleb

Reputation: 137

How to change the state automatically depends on a date ? (odoo 11)

how to change status/status automatically according to a given date on the form, I tried this code but it doesn't work:

@api.multi
def action_confirm(self):
    for rec in self:
        if rec.mission_start_date:
            td = datetime.now().strftime("%Y-%m-%d")
            today = datetime.strptime(td, "%Y-%m-%d")
            start = datetime.strptime(rec.mission_start_date, "%Y-%m-%d")
            if start == today:
                res = self.write({'state': 'progress'})
    return res

Upvotes: 0

Views: 1304

Answers (2)

Kenly
Kenly

Reputation: 26678

You need to define a scheduled action and set interval_type to days:

1 - Create a new scheduled action:

    <record id="ir_cron_scheduler_missions" model="ir.cron">
        <field name="name">Mission scheduler</field>
        <field name="user_id" ref="base.user_root"/>
        <field name="interval_number">1</field>
        <field name="interval_type">days</field>
        <field name="numbercall">-1</field>
        <field eval="False" name="doall"/>
        <field eval="'mission.mission'" name="model"/>
        <field eval="'process_scheduler_queue'" name="function"/>
    </record>

2 - Define the function in the scheduled action model:

class Mission(models.Model):
    _name = 'mission.mission'

    @api.model
    def process_scheduler_queue(self):
        for rec in self.search([('state', '!=', 'progress')]):
            if rec.mission_start_date and rec.mission_start_date == fields.Date.today():
                rec.write({'state': 'progress'})

Upvotes: 1

Younis Mahsud
Younis Mahsud

Reputation: 517

For this purpose you can use scheduled actions. Scheduled actions runs during time interval which you set. Please read about scheduled actions here: https://www.odoo.yenthevg.com/creating-automated-actions-in-odoo/

Upvotes: 1

Related Questions