Setting dates corresponding to another date field

im working on a doctype shown below. i want to set date in the field “Delivery Date” 15 days ahead of date in the “Date” field (current date).

i have use the following code,

number_of_days = 15;
delivery_date = date.today() + timedelta(days=(number_of_days))
doc.delivery_date = delivery_date

the code will work if it is in the validate, but i want this to work in after_insert (which is not working).

is there any solution for this?

@Rohith_Mohanan you need js to change the value right after insertion. so custom scripts is your answer . or better just write the code in your doctype_name.js file .

frappe.ui.form.on('Server', {
    date : function(frm) {
    //this function will run on date change. 
    var old_date = frm.doc.date;
   //write your code to change date , you can call frappe.utils.add_to_date()

    frm.set_value("delivery_date",new_date);
    refresh_field("delivery_date");
    }
});
1 Like

didnt work, im not good with js. so can you be more specific about the code?
thank you

@Rohith_Mohanan the code is missing the part where you calculate the difference

1 Like

Event after_insert happens after the doc has been saved.
So try to manually call save() like this:

number_of_days = 15;
delivery_date = date.today() + timedelta(days=(number_of_days))
doc.delivery_date = delivery_date
doc.save()

You may also try to directly set the value in the database using frappe.db.set_value:

number_of_days = 15;
delivery_date = date.today() + timedelta(days=(number_of_days))
frappe.db.set_value(‘YourDoctype’, doc.name, ‘delivery_date’, delivery_date )

https://frappeframework.com/docs/v13/user/en/api/database#frappedbset_value

1 Like