Skip to Content
Menu
This question has been flagged

Hi all, please help me to write onchange function in timesheet app.

I have 2 fields. 

one is base field that is Hours spent(unit_amount)

another one custom field that i created is bud(x_studio_bud)

This custom field value should not exceed base field value. If it exceed i need to show an error.

Example: 

base field value is 10.00

if i type greater value above than base field value. for example 10.10, it should show an error message.


Please help me to do this. I am using ODOO 15 enterprise edition.

Many thanks


Avatar
Discard
Best Answer

Hi Abdul Razak,

You can use the below code with your field names to show an error message.

from odoo import _
from odoo.exceptions import UserError


@api.onchange('base_field', 'custom_field')
def onchange_field(self):
if self.base_field and self.custom_field > self.base_field:
raise UserError(_("CUSTOM FIELD value should be less than BASE FIELD value."))

Regards

Avatar
Discard
Author

Hi many thanks for the reply.

I am using enterprise edition 15. i have pasted this code in the compute field. I have pasted the exact code which is above and I have replace the field name. here is my code and editing.

from odoo import _
from odoo.exceptions import UserError

@api.onchange('unit_amount', 'x_studio_budget_hours')
def onchange_field(self):
if self.unit_amount and self.x_studio_budget_hours > self.unit_amount:
raise UserError(_("Value exceeded"))

but iam facing error.
ERROR:
Traceback (most recent call last):
File "/home/odoo/src/odoo/15.0/odoo/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/odoo/src/odoo/15.0/odoo/http.py", line 301, in _handle_exception
raise exception.with_traceback(None) from new_cause
ValueError: forbidden opcode(s) in 'from odoo import _\nfrom odoo.exceptions import UserError\n\n\n@api.onchange(\'unit_amount\', \'x_studio_budget_hours\')\ndef onchange_field(self):\n if self.unit_amount and self.x_studio_budget_hours > self.unit_amount:\n raise UserError(_("Value exceeded"))': IMPORT_NAME, IMPORT_FROM

Please help....

Related Posts Replies Views Activity
1
Oct 24
1067
1
Jun 23
1419
1
Jan 22
1730
5
Jun 17
4062
1
May 23
1241