Custom Script for Batch Expiry Days

frappe.ui.form.on('Batch', {
    expiry_date: function(frm, cdt, cdn){
        var d = locals[cdt][cdn];
        // put any logic here
        var expires_in_days = frappe.datetime.get_day_diff(d.expiry_date, frappe.datetime.nowdate());
        // update the field
	frappe.model.set_value(cdt, cdn, 'expires_in_days', expires_in_days);
      refresh_field("expires_in_days");
    }
});

I created the above client script for Batch doctype to calculate the remaining days to expire in a custom field named “Expiry In Days” based on expiry date . The script works well when a new batch is created or when we manually re enters the expiry date of an existing batch.
What i want to achieve is that the script should update the custom field “Expiry In Days” automatically every day which it does not do. Then value of the custom field remains same as the value generated for the first time.

Appreciate if anyone can help in achieving my requirement.

@peterg

1 Like

use a daily scheduler event and write the code in python.

Can you help me with that if possible. I have no idea about it. I tried doing few things reading the documentation but went over my head. I could not succeed.

get list of all Batches. for every batch do your changes .

batches = frappe.db.get_list('Batch')
for b in batches : 
     doc = frappe.get_doc('Batch', b["name"])
     doc.expires_in_days = (your code here)
     doc.save()

you can open (bench console) and write the code there to test it . if it works move it to a scheduler event.

some erpnext python documentation :
Document API
Database API