Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
2465 Weergaven

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
Annuleer
Beste antwoord

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
Annuleer
Auteur

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

Gerelateerde posts Antwoorden Weergaven Activiteit
1
okt. 24
2168
1
jun. 23
2126
1
jan. 22
2750
5
jun. 17
5139
1
mei 23
2387