How to Auto Cancel and Recreate Sale Order with Cron Schedule on Odoo 17?

76 views Asked by At

The system will create a corresponding cron schedule when I create an order. If after 10 minutes, the order has no changes, then that cron schedule will automatically activate and automatically delete when it has completed the schedule. Please suggest some technical solutions.

I have successfully created a cron schedule after creating a row, however, I still don't know how to make it automatically delete after completing the schedule.

2

There are 2 answers

1
Nkosikhona Carlos On

The answer will be a bit broader but to summarise what you need to do.

  1. With your cron job (Python) on your file you will need to first search or filter your quotations. You can do this with Python datetime, i.e. filter quotation created today.

  2. Remember to unlink in case you are not allowed to delete. this you can achieve by

    try: ... except: ...

  3. Odoo does have a changed date field which will come in handy.

Hope this helps

1
Talha Khan On

You can create a Cron Job and schedule it to run every 10 minutes.

Cron function Logic:

In the Python code within the Cron function, search for all draft sale order records, check and compare their creation time. If the time difference is greater than 10 minutes, execute a delete query for that sale order.

Example:

def delete_sale_orders(self):
    ten_minutes_ago = fields.Datetime.to_string(fields.Datetime.now() - timedelta(minutes=10))
    draft_orders = self.env['sale.order'].search([('state', '=', 'draft'), ('create_date', '<', ten_minutes_ago)])
    draft_orders.unlink()

Hope this will help :)