How to fetch value from Database in a Doctype Field

Hi Everyone,
How can we fetch values from database into a doctype field?

In no of working days field I want to fetch how many days does an employee present in a month

hello, you can use custom script to fetch value from db and insert it in any field,
change my_local_field & my_remote_field to your target fields:

frappe.ui.form.on('Employee', {
    refresh: async function(frm) {
        let result = await frappe.db.get_value("Employee", frm.doc.name, "my_remote_field");
        frm.doc.my_local_field = result.message.my_remote_field

        frm.refresh_fields();
    }
})

Thanks… Can I ask you one question?

change my_local_field & my_remote_field to your target fields:

what does this mean? and I want to get values on Attendance doctype so I have to change Employee doctype to Attendance right?? Actually I am new and don’t know much about coding…

1 Like

welcome, right for example to get the value of Attendance ((attendance_date)) field and put it in Employee (last_name) field we do:

frappe.ui.form.on('Employee', {
    refresh: async function(frm) {
        let result = await frappe.db.get_value("Attendance", {employee: frm.doc.name}, "attendance_date")
        
        frm.doc.last_name = result.message.attendance_date

        frm.refresh_fields();
    }
})
  • 1 we use get_value method to send fetch request to the server and we filter Attendance entries by the employee field which is in the target doctype using our local employee name (frm.doc.name)

  • 2 we exctract the field value from the response message object and put it in the last_name of the Employee doctype

  • 3 finally refresh fields to show the new value

Thanks… Actually I don’t want to fetch any value from another doctype what I want is to fetch total of days an employee present on a month… I think this could be achieve from fetching values directly from database and then show the result in No of working days field…