author: @kawam tags:#odoo#odoo-studio


Sudo

To make a server action run with elevated privileges using sudo in Odoo, you can modify the Python code in the server action to include the sudo() method. Here’s an example of how to modify the code:

# Define the Python code to execute with sudo record = 
self.env['model.name'].sudo().browse(record_id) 
 
record.do_something() 
# Add the sudo() method to the server action

Passing context from button to server action

<button string="Button Label" type="object" name="button_function" context="{'key': 'value'}" />

and in server action:

category = env.context.get('category')

Passing ID from another model with context

active_id adalah variabel yang berisi ID dari record yang dipilih ketika aksi dilakukan. Variabel ini hanya tersedia dalam lingkup method (fungsi) yang berjalan pada tampilan aktif, seperti pada tombol aksi server (server action) atau method kontroler (controller method).

record = env[category].sudo().search([('id', '=', env.context.get('active_ids'))], limit=1)