2

I'm running Odoo 12 and I'd like to set up an automated action after a new Opportunity is created. This automated action would be some custom Python code which calls a system parameter, gets its value and stores it in a field in the new opportunity's record. Afterwards, that system parameter should be increased by 1 so another new opportunity doesn't have the same number.

How do you get/set a system parameter from python code?

I've created a system parameter with key "customParameter"and value 10122. The field where this parameter should be put is called "x_studio_deal_quotation_id", within a new opportunity.

I've got a little experience in Python, so I've no idea how to call upon this parameter. Can someone help me out? Or is there an easier way to achieve the same? A big thanks in advance.

arajshree
  • 626
  • 4
  • 13
Erwin
  • 33
  • 7
  • Hello and welcome on Stack Overflow. Could you give an [example of your code](https://stackoverflow.com/help/minimal-reproducible-example), with the expected result and the current result ? – Bertrand Gazanion Jun 27 '19 at 09:19
  • @BertrandGazanion I simply tried`record[("x_studio_deal_quotation_id")] = customParameter customParameter += 1` and the output error I get when I create a new opportunity is: _ValueError: : "name 'customParameter' is not defined" while evaluating_ – Erwin Jun 27 '19 at 09:59

1 Answers1

1

I managed to find what I'm looking for on my own, this is the code I used:

record[("x_studio_deal_quotation_id")] = record.env['ir.config_parameter'].get_param('customParameter')
var = record.env['ir.config_parameter'].get_param('customParameter')
var = int(var)
var += 1
record.env['ir.config_parameter'].set_param('customParameter', var)

In this function I called my system parameter by using record.env['ir.config_parameter'].get_param('customParameter') I had to convert it to int because I got some error that the type wasn't right. Finally, I add 1 to my variable value and write the same system parameter with the new value by using:record.env['ir.config_parameter'].set_param('customParameter', var)

Erwin
  • 33
  • 7
  • Why not using Odoo's sequences (ir.sequence) for this? It's more powerful than using a simple system parameter. – CZoellner Jun 27 '19 at 20:24