1
votes

I want to notify the user that a record has been updated so I create a boolean field that indicates the change. I also color coded the tree so that when the boolean field is set true the color will change to notify the user.

Now I have to update the boolean field when the user click on the record in the tree view and open the form view. How do I do that?

Here is a dummy model and a dummy view I created:

Model:

    from openerp import models, fields, api, _

    class form_record_update_test_subject_a(models.Model):

        _name           = "form.record.update.test.subject.a"

        name            = fields.Char('Name')
        is_blue         = fields.Boolean('Is Blue')

    form_record_update_test_subject_a()

View:

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <menuitem name="Testing Module"
            id="testing_module_menu"
            sequence="1"/>

        <menuitem id="form_record_update_menu" name="Form Record Update Test" parent="testing_module_menu" sequence="1"/>

        <record model="ir.ui.view" id="form_record_update_test_subject_a_form_view">
            <field name="name">form.record.update.test.subject.a.form.view</field>
            <field name="model">form.record.update.test.subject.a</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Form Request Update Test" version="8.0">
                    <sheet>
                        <group>
                            <field string="Name" name="name" class="oe_inline"/>
                            <field string="Is Blue" name="is_blue" class="oe_inline"/>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <record model="ir.ui.view" id="form_record_update_test_subject_a_tree_view">
            <field name="name">form.record.update.test.subject.a.tree.view</field>
            <field name="model">form.record.update.test.subject.a</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Class" colors="blue:is_blue==True">
                    <field string="Name" name="name"/>
                    <field string="Is Blue" name="is_blue"/>
                </tree>
            </field>
        </record>

        <record model="ir.ui.view" id="form_record_update_test_subject_a_search">
            <field name="name">form.record.update.test.subject.a.search</field>
            <field name="model">form.record.update.test.subject.a</field>
            <field name="type">search</field>
            <field name="arch" type="xml">
                <search string="Form Request Update Test Search">
                    <field string="Name" name="name"/>
                    <field string="Is Blue" name="is_blue"/>
                </search>
            </field>
        </record>

        <record id="form_record_update_test_subject_a_action" model="ir.actions.act_window">
            <field name="name">Form Request Update Test</field>
            <field name="res_model">form.record.update.test.subject.a</field>
            <field name="view_type">form</field>
            <field name="domain">[]</field>
            <field name="context">{}</field>
            <field name="view_id" eval="form_record_update_test_subject_a_tree_view"/>
            <field name="search_view_id" ref="form_record_update_test_subject_a_search"/>
            <field name="target">current</field>
            <field name="help">Form Request Update Test</field>
        </record>

        <menuitem action="form_record_update_test_subject_a_action" icon="STOCK_JUSTIFY_FILL" sequence="1"
            id="form_record_update_test_subject_a_action_menu"  parent="testing_module.form_record_update_menu"
        />
    </data>
</openerp>
1
Which part of my description you don't understand? Choose a record from tree, click, open form, update the record.William Wino
you can override the write function and set the boolean value there. Because you only will want to change the boolean field if record is updated.Arsalan Sherwani
@ArsalanSherwani nope, I want to update the boolean field when the record is viewed from the form view.William Wino
@William Did you figure this out?Raja Ehtesham

1 Answers

0
votes

As I stumbled over this question when looking for a similar solution and finally figured it out by myself I would like to share my solution:

You will need to register your own JS asset. Create a js-file in you static/src/js folder in your module. Register it:

    <template id="assets_backend" name="MODULENAME assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/YOUR_MODULE_NAME/static/src/js/FILENAME.js" />
        </xpath>
    </template>

Inside the javascript you will need to write the following:

odoo.define('dsw_dongle.sheet', function (require) {
"use strict";
var Model = require('web.Model');
// register here the model you would like to change
var my_model = new Model('YOUR_MODEL_NAME');
var FormView = require('web.FormView');
var framework = require('web.framework');

var myForm = FormView.include({
    load_record: function(record) {
        // This will be triggered every time the data inside the form will be loadd
        this._super.apply(this, arguments);
        // make sure that this is your view either by pulling it from 'this'
        // or from the 'record'. For demo I skipped this.
        var self = this;

        // If your update-procedure is taking longer, block the UI
        framework.blockUI();
        dongle_model.call('MODEL_FUNCTION_NAME', [record.id]).then(function(resp){
            // make sure that you return a nice value otherwise this will be 
            // an endless recursion
            if (resp === true) {
                self.reload();
            } else {
                // Remove the waiting screen
                framework.unblockUI();
            }
        });
     }
  });
});

Hope this will help others looking for an "on_load"-Event in Odoo 10.0