Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
3918 Zobrazení

I am trying to show a custom (warning) message upon clicking confirm button in Invoices (account.move). 

The basic idea is: when user confirms new invoice check condition (is this the first time showing message?) and raise UserError if condition is true otherwise continue and create invoice. 

This works fine, but i have bool field serving as a flag (Was message already shown?) and i need to update this flag (with permanent change) before raising UserError. Unfortunately raising UserError causes database rollback so my update of flag field is always rollbacked.

I came across post on Stack Overflow explaining Odoo cursor and raw SQL queries and it seemed like it might be an answer to my problem but i have no idea how to do it.

I want to update only one field (flag field) and not commit whole record.

Avatar
Zrušit
Nejlepší odpověď

you can make use of the Odoo cr (database cursor) object and execute raw SQL queries. Here's an example of how you can update a single field and avoid the rollback caused by raising a UserError:

from odoo.exceptions import UserError

# ...

def your_confirm_method(self):
# Get the current record's ID
record_id = self.id

# Get the database cursor
cr = self.env.cr

# Update the field directly using a raw SQL query
query = """
UPDATE account_move
SET your_flag_field = true
WHERE id = %s
"""
cr.execute(query, (record_id,))

# Check your condition and raise UserError if needed
if your_condition:
raise UserError("Your warning message")

# Continue with the normal invoice confirmation process
# ...

In the above code, replace your_flag_field with the actual name of your flag field in the account_move table. Make sure to replace your_condition with your actual condition for showing the warning message.

By using the database cursor (cr) and executing a raw SQL query, you can update the specific field directly in the database without relying on the ORM's transaction handling.

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
kvě 22
6804
0
čvn 21
6344
1
čvc 25
2278
2
čvc 25
7753
2
čvc 25
4169