Skip to Content
Menu
This question has been flagged
1 Reply
3608 Views

Hi everyone,

I want to have a button on one of my form views that triggers a function in the backend. This function has a couple of parameters, and I want these parameters to be passed live by the user, when clicking the button.

Is there a way to implement this on Odoo? The perfect scenario would be:

  • the user clicks the "Trigger Function" button
  • a prompt opens up with two fields (can be a TransientModel, although I never want the user input to be stored in the database, so ideally it should be passed directly to the backend)
  • the function runs with these two parameters, and the output of the function is used to fill a field in the original record
  • the original form view stays open

Any ideas would be greatly appreciated


Avatar
Discard
Best Answer

Yes you can do this through Odoo Wizards here is my full code in github gists.

To make a wizard you have to use the TransientModel witch will store the data temporarily in the database and it will be vacuumed after a while 

in your_module/wizards/wizard.py

class PopupWizard(models.TransientModel):
    _name = 'popup.wizard'
    
    title = fields.Char()
    start_date = fields.Datetime()
    
    def confirm(self):
      # to get the title value
      your_variable = self.title       # do what you want with the self.env to update any record you want    
      return {
            'name': "Your action name",
            'type': "ir.actions.act_window",
            'res_model': "your_model,
            'view_type': "form",
            'view_mode': "tree,form",
            'form_view_id': "your_form_external_id"
            'help': "help text",
       }


then in your form you can create a button that takes the action id with the %d function 


Happy to help :) an upvote will be awesome

Avatar
Discard
Author

Hi, I really appreciate your comment :-) this is the approach I was thinking about following, as I've written a few wizards, but at the time I didn't understand how to avoid having to write the input to the database (which can easily be prevented by overriding the create method).

My situation at the moment is the following: I have my model (Model X) and this model has a button in each record's form view. This button needs to:

* open the form view for the PopupWizard, which I believe can be done be returning the right action in the Python code
* the user inserts some input into the popup wizard and presses the confirm button (as you have done in your gist)
* upon pressing the confirm button, the code needs to go back to the original record of Model X and modify it in a certain way

My question is: how do I keep track of the ID of the original record? Is it possible to do this by passing context to the Popup Wizard action?

Thanks!

When you click on the done button on your wizard you can return an action from your wizard method and it will open the form again for you just like refresh i updated my answer with the action

Related Posts Replies Views Activity
1
Jun 23
1376
1
Jun 22
2363
2
Jul 15
6208
0
Dec 24
678
1
Apr 23
111