This question has been flagged
5 Replies
14669 Views

This field is left blank unless the user fills it in - I'd like to fill it in automatically.

How can this be achieved?


Avatar
Discard
Best Answer

@David i managed it using ur method

Action To  Do:  Update the record

Trigger Condition: On Creation

Date to write:

Field: Expiration (Sales Order)

Evaluation Type: Python expression

Value:  record.date_order + datetime.timedelta(days=30)


Avatar
Discard

Thank you, I was able to get this implemented after years of not having it. I would like to add that in Odoo v16 the model "Quotation" doesn't seem to exist. The model instead is "Sales Order" as suggested by the field value you listed, "Expiration (Sales Order)".

Best Answer

You can set the value of a field using an Automated Action, configured with a Trigger Condition of On Creation.

1. Activate Developer Mode, then nagivate to Settings --> Technical --> Automation --> Automated Actions.

2. Create an Automated Action like this:


The Python script portion:

DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"

if not record.validity_date:
record['validity_date'] = datetime.datetime.strptime(record.date_order, DATETIME_FORMAT)
+ datetime.timedelta(days=30)

This example checks if the user has defined an Expiry Date, and if they have not, calculates a default 30 days from now.

By changing 30 to 60 you can extend the default number of days used to calculate the Expiry Date of the Quotation.

Avatar
Discard

dear Ray

Im using Saas v13 enterprise and tried the same steps and code you mentioned however its giving below error

Error:

Odoo Server Error

Traceback (most recent call last):

File "/home/odoo/src/odoo/13.0/odoo/tools/safe_eval.py", line 349, in safe_eval

return unsafe_eval(c, globals_dict, locals_dict)

File "", line 13, in <module>

TypeError: strptime() argument 1 must be str, not datetime.datetime

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/odoo/src/odoo/13.0/odoo/http.py", line 619, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "/home/odoo/src/odoo/13.0/odoo/http.py", line 309, in _handle_exception

raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

File "/home/odoo/src/odoo/13.0/odoo/tools/pycompat.py", line 14, in reraise

raise value

File "/home/odoo/src/odoo/13.0/odoo/http.py", line 664, in dispatch

result = self._call_function(**self.params)

File "/home/odoo/src/odoo/13.0/odoo/http.py", line 345, in _call_function

return checked_call(self.db, *args, **kwargs)

File "/home/odoo/src/odoo/13.0/odoo/service/model.py", line 93, in wrapper

return f(dbname, *args, **kwargs)

File "/home/odoo/src/odoo/13.0/odoo/http.py", line 338, in checked_call

result = self.endpoint(*a, **kw)

File "/home/odoo/src/odoo/13.0/odoo/http.py", line 910, in __call__

return self.method(*args, **kw)

File "/home/odoo/src/odoo/13.0/odoo/http.py", line 510, in response_wrap

response = f(*args, **kw)

File "/home/odoo/src/odoo/13.0/addons/web/controllers/main.py", line 1320, in call_kw

return self._call_kw(model, method, args, kwargs)

File "/home/odoo/src/odoo/13.0/addons/web/controllers/main.py", line 1312, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "/home/odoo/src/odoo/13.0/odoo/api.py", line 387, in call_kw

result = _call_kw_multi(method, model, args, kwargs)

File "/home/odoo/src/odoo/13.0/odoo/api.py", line 374, in _call_kw_multi

result = method(recs, *args, **kwargs)

File "/home/odoo/src/odoo/13.0/addons/base_automation/models/base_automation.py", line 291, in write

action._process(records, domain_post=domain_post)

File "/home/odoo/src/odoo/13.0/addons/base_automation/models/base_automation.py", line 221, in _process

raise e

File "/home/odoo/src/odoo/13.0/addons/base_automation/models/base_automation.py", line 218, in _process

self.action_server_id.with_context(**ctx).run()

File "/home/odoo/src/odoo/13.0/odoo/addons/base/models/ir_actions.py", line 545, in run

res = func(action, eval_context=eval_context)

File "/home/odoo/src/odoo/13.0/odoo/addons/base/models/ir_actions.py", line 430, in run_action_code_multi

safe_eval(action.sudo().code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action'

File "/home/odoo/src/odoo/13.0/odoo/tools/safe_eval.py", line 372, in safe_eval

pycompat.reraise(ValueError, ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr)), exc_info[2])

File "/home/odoo/src/odoo/13.0/odoo/tools/pycompat.py", line 13, in reraise

raise value.with_traceback(tb)

File "/home/odoo/src/odoo/13.0/odoo/tools/safe_eval.py", line 349, in safe_eval

return unsafe_eval(c, globals_dict, locals_dict)

File "", line 13, in <module>

ValueError: <class 'TypeError'>: "strptime() argument 1 must be str, not datetime.datetime" while evaluating

'# Available variables:\n# - env: Odoo Environment on which the action is triggered\n# - model: Odoo Model of the record on which the action is triggered; is a void recordset\n# - record: record on which the action is triggered; may be void\n# - records: recordset of all records on which the action is triggered in multi-mode; may be void\n# - time, datetime, dateutil, timezone: useful Python libraries\n# - log: log(message, level=\'info\'): logging function to record debug information in ir.logging table\n# - Warning: Warning Exception to use with raise\n# To return an action, assign: action = {...}\n\nDATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"\n\nrecord[\'validity_date\'] = datetime.datetime.strptime(record.date_order, DATETIME_FORMAT) + datetime.timedelta(days=30)'

Best Answer

No need to use automated actions. There is a parameter in settings / sales. 
(Screenshot from V16)


Avatar
Discard
Best Answer

I only see "Scheduled Actions" and no "Automated Actions" in my odoo instance (v11, community). Is this an enterprise feature ?

Avatar
Discard

Check if you have the module "Automated Action Rules" (aka base_automation) installed.

Ah, that was not installed ! Thanks again, Ray!

Best Answer

Hello there!

I solve this problem in this way:


I made  an  automated action...

Action To  Do:  U pdate the record

Trigger Condition: On Creation

Date to write:

Field: Expiration (Sales Order)

Evaluation Type: Python expression

Value:  record.date_order + datetime.timedelta(days=30)


Hope this help for somebody. :)

Avatar
Discard