Updating field value based on another doctype field value,

I have link field in my custom doctype where it is linked with core module(Task). I have written custom script to trigger onchange event for my field. But i need to update form value whenever the core doctype value is changed. How to achieve this?

Is there any custom script to achieve this?

Thanks in advance

Hi @ibalajib,

Please share your code.

working_id: function(frm,cdt,cdn) {
var test = locals[cdt][cdn];
frappe.db.get_value(‘Holiday List’,{‘name’:test.working_id}, [‘from_date’, ‘to_date’, ‘total_holidays’ ], function(value){
var from_date = value.from_date
var to_date = value.to_date
var holidays = frappe.datetime.get_day_diff(to_date, from_date)
console.log(holidays)
var final_holidays = holidays - value.total_holidays + 1
test.duration = final_holidays
refresh_field(‘duration’)
if (test.working_id) {
test.number_of_leaves = value.total_holidays
refresh_field(‘number_of_leaves’)
}

	})

}

});

The above is my code. So whenever I modify holidays in Holidays list(core) I want that change to happened in my saved records. Thanks @NCP

Hi @ibalajib,

Automatically update for use server script like this:
Document Type: Holiday List
DocType Event: After Save Or Before Save (Set and check your according.)

# EDIT Code
test_name = frappe.db.get_value("Custom DocType",{'working_id':doc.name},'name')
if test_name:
    test_doc = frappe.get_doc("Custom DocType",test_name)
    from_date = doc.from_date;
    to_date = doc.to_date;
    holidays = frappe.datetime.get_day_diff(to_date, from_date);
    final_holidays = holidays - doc.total_holidays + 1;
    test_doc.duration = final_holidays;
    test_doc.number_of_leaves = doc.total_holidays;

Do not remove the custom/client script.

Please apply and check it.

Thank You!

1 Like

Thank you so much for the code @NCP

Sorry I’m a newbie to Erpnext I have some doubts again.
Do you want me to use Doctype event?

I mean should i create new server script and paste this? Does this code affect core module code?

Your client is perfect but changes events for use server script because you change in holiday list then after effect on custom doctype.

Yes.

Check-in Holiday list change then save it then effect on custom doctype or not.
but if working_id == holiday name

Thanks.

Yeah i understood that. Thanks @NCP My doubt is if i create server script for Holiday List does that affect core module code?

Yes.
Set your variable according to.
If not work then disable it and some demo server script check.

Thanks.

Thanks for the response @NCP I understood and I’ll check it out.

Hi @NCP Could you help me with my latest topic?

Thanks :slight_smile:

but can you try this:

And what error show?
Please share.

Hi @ibalajib,

If not work then try this, please.
It’s working on my side.
So apply and check it.


Document Type: Holiday List
DocType Event: After Save


test_name = frappe.db.get_value("Custom DocType",{'working_id':doc.name}, 'name');
if test_name:
    test_doc = frappe.get_doc("Custom DocType",test_name)
    holidays = frappe.utils.date_diff(doc.to_date, doc.from_date)
    final_holidays = holidays - doc.total_holidays + 1;
    test_doc.duration = final_holidays;
    test_doc.number_of_leaves = doc.total_holidays;
    test_doc.save()

Custom DocType = You can also use core doctype or set doctype which you can update a value in DocType.

Thank You!

No @NCP I’m gonna try this one. This one is clear. I’m saying that i’ve posted another topic regarding doubts on ERP Next in the forum.