This question has been flagged
4 Replies
14009 Views

Hi, I am learning to write a schedule action and i have a problem at field 'nextcall', Here is my code:

<record id="cron_do_task" forcecreate='True' model="ir.cron">

<field name="name">Check Bed</field>

<field eval="True" name="active"/>

<field name="user_id" ref="base.user_root"/>

<field name="interval_number">1</field>

<field name="interval_type">days</field>

<field name="nextcall" eval="datetime.now(pytz.timezone('UTC')).strftime('%Y-%m-%d 23:59:59')" />

<field name="numbercall">-1</field>

<field name="model_id" ref="model_check_bed"/>

<field name="state">code</field>

<field name="code">model.cron_do_task()</field>

</record>

This code is right, but nextcall return Time in UTC. I want nextcall to be time with my timezone ('Asia/Ho_Chi_Minh'), so i tried:  datetime.now(pytz.timezone(self._context.get('tz') or 'UTC')).strftime('%Y-%m-%d 23:59:59'), but i got error because xml can't define 'self'. How can i get timezone's user in xml file? Please suggest me some solutions.

Thanks and Regards,

Minh

Avatar
Discard
Best Answer

Could you try this code?

<field name="nextcall" eval="datetime.now() + (datetime.now(pytz.timezone('Aisa/Ho_Chi_Minh')).replace(hour=23, minute=59, second=59) - datetime.now(pytz.timezone('Aisa/Ho_Chi_Minh'))) % timedelta(hours=24)"/>

Avatar
Discard

I tested this on Odoo14, and it working correctly,

Thank you,
Tri

Worked for me in odoo 15, but you should replace 'Asia/Ho_Chi_Minh' with 'Aisa/Ho_Chi_Minh' or else it will be error. This is a spelling issue.

With this answer, can i make it to nextcall will be come 2nd day after today? i've tried datetime.now() + datetime.now() but not working.

Best Answer

I think u can try manual minus hour for 7, smt like this:

<field name="nextcall" eval="datetime.now(pytz.timezone('UTC')).strftime('%Y-%m-%d 16:59:59')" />
Hope that help!

Avatar
Discard
Author

Hi Cuu Nguyen, I think that only good for gmt+7 , if we change timezone user, it willnot be ok and cant slove my issue.

Well, in that case I think u can use function to (re)set "nextcall" field after stored ur cron record. For example:

<function name="method_to_call" model="model_contains_method"/>

and ur method is smt like this:

def method_to_call(self):

ir_record = self.env.ref("module_name.cron_do_task")

ir_record.nexcall = <next time that u want it to be call - computed by yourself>

with cron_do_task is xml id of ur record and module_name is, well, I think u knew it :)