How to use Schedule Script at a specific time

Hi,

I want to run a code at a specific time,
my case is to change the item status to enable at 8 AM and change to disabled at 8 AM

How I can do this??

Hello,

You have to make a custom app and declare a cron.

Look at hook.py of ERPNext App there is plently of sample

2 Likes

cron expression is the only way but that requires custom app.

If you only want to do it with server script then use hourly job but check that you’re on 8 AM timespan to execute your code.

Pseudocode:

if now().hour() == 8:
    frappe.db.set_value("Item", ... "enabled", 0)
elif now().hour() == 20:
    frappe.db.set_value("Item", ... "enabled", 1)
3 Likes

@ankush
Thank u Man,

but which function gets the Current hour()?
I found this

frappe.utils.nowtime

this code is working good but with Custom app not with Server Script

import datetime

if(str(datetime.datetime.now().hour) == 11):
    item = frappe.get_doc("Item", "BC-DP-CON-1000448")
    item.disabled = 1
    item.save()

how I implemented in server script

You can’t use import from server script. There are a list of avaliable methods here:
https://docs.erpnext.com/docs/user/manual/en/customize-erpnext/server-script#23-security

Use this:

if(frappe.utils.format_time(frappe.utils.get_datetime(),"HH") == "11"):
    item = frappe.get_doc("Item", "BC-DP-CON-1000448")
    item.disabled = 1
    item.save()

Hope this helps.

3 Likes

Thank u so much, @avc

u make my day.

1 Like

Server scripts also support CRON now btw

image

2 Likes

Thank you so much bro
@ankush