Skip to Content
Menu
This question has been flagged
4 Replies
2373 Views

Hello, i would like to know how to apply updates in a custom field once at the end of every month?

thanks in advance.


Avatar
Discard

please explain in detail what you want to update.

Best Answer

Hi,

You could do this through a server action or an automated action. An example:

<?xml version="1.0"?>
<odoo>
    <data noupdate="1">
        <!-- Scheduler for updating field once a month -->
        <record forcecreate="True" id="ir_cron_custom_monthly_update" model="ir.cron">
            <field name="name">Custom: update own field on monthly basis</field>
            <field name="model_id" ref="model_your_model"/>
            <field name="state">code</field>
            <field name="code">model.update_records()</field>
            <field eval="True" name="active"/>
            <field name="user_id" ref="base.user_root"/>
            <field name="interval_number">1</field>
            <field name="interval_type">months</field>
            <field name="numbercall">-1</field>
            <field eval="False" name="doall" />
        </record>
    </data>
</odoo>

Then write a Python function that binds to this scheduler and executes the code you'd like:

@api.model
def update_records(self):
    records = self.env['your.model'].search([])
    for record in record:
record.write({'your_field': 'new_value'})


Regards,
Yenthe

Avatar
Discard
Author Best Answer

Thank you so much guys for your help 

Avatar
Discard
Best Answer

Hello Mazen,

use write method on your model

@api.multi

def write(self, vals):

    if vals.get('your custom field', false):

        ##here you can update some other log table or do your logic

    return super(your calss name, self).write(vals)

Avatar
Discard