Fetch Values From Employee To Leave Application Based on Employee ID

Hi,
I want to fetch values of branch and department from Employee
to Leave Application,
I’ve added Branch and Department link fields in Leave Application for applying permissions,
But An Employee have to set their branch and department by himself,
I want system should pick their branch and department automatically based on EMP ID.

1 Like

Hi, please try to add the script to Leave Application

frappe.ui.form.on("Leave Application", {    
    cur_frm.add_fetch("employee", "branch", "branch")
    cur_frm.add_fetch("employee", "department", "department")
})

When this function will run?

I need to run the function when user create new application.

@NMyshuk thanks buddy but your script is not working,
i’ve tried this

frappe.ui.form.on(“Leave Application”, {
leave_type: function(frm){
add_und(frm);
}
});

var add_und = function(frm)
{
cur_frm.add_fetch(“employee”, “branch”, “unit”);
cur_frm.add_fetch(“employee”, “department”, “department”);
}

but it is not working when i select leave type,
just working on when i select employee, and system automatically selects employee by default, when system selects by default, function didn’t run, when i erase employee id and retype my id then it picks my branch and department,
Note : field name “UNIT” is linked to branch. means UNIT = BRANCH.

Hi @shahid,

the thing with add_fetch is that it only works on data that is cached. It might be that in your case, the more robust way will be to go through a database lookup. Use something like this:

frappe.call({
    "method": "frappe.client.get",
    "args": {
         "doctype": "Employee",
         "name": frm.doc.employee
    },
    "callback": function(response) {
         var employee = response.message;
         if (employee) {
              frm.set_value('unit', employee.branch);
         } else {
              frappe.msgprint("Employee not found");
         }
    }
}); 

This only requires your form to have a valid employee set and will get the employee record. From this you can the pull fields to your form. Adapt if you want to get other data.

Hope this helps.

@lasalesi it is not working.
Capture

Can you please post your current script? It seems like you are missing to pass the frm object…

my script runs when you choose Employee.

also you can use cur_frm.doc.employee

1 Like

@lasalesi i’ve just copy and paste your script without any editing.

frappe.call({
“method”: “frappe.client.get”,
“args”: {
“doctype”: “Employee”,
“name”: frm.doc.employee
},
“callback”: function(response) {
var employee = response.message;
if (employee) {
frm.set_value(‘unit’, employee.branch);
} else {
frappe.msgprint(“Employee not found”);
}
}
});

@NMyshuk i’ve tried your script but didn’t work, so i make some changes to it, then its working but it is only working someone selects employee, but as a normal user, when i go to leave application, system is selecting my employee by itself when he make new leave application, but it is not selecting the unit and department, it is only selecting unit and department when he make change to employee field.
have you got my point?

Note : the script i’ve customized is next to your script.

Hi, it’s very-very strange, because from my side it works always, even you as a normal user and go to leave application and system is selecting my employee by itself.
but my script had the error. it shouldn’t be in the ‘frappe.ui.form.on(“Leave Application”, {})’
please check image

Yes system is selecting employee by itself but didn’t picking their unit and department,
it picks unit and department when i make change to employee field.
i want to pick up their unit and department when user click on new leave application button,
because once employee is selected by system automatically then user won’t to go to employee field to change it, and probably they shouldn’t.

Again on my side it works even employee is selected by system automatically.

Please try this script

frappe.ui.form.on("Leave Application", "onload", function(frm) {
    frappe.call({
        "method": "frappe.client.get",
        "args": {
             "doctype": "Employee",
             "name": frm.doc.employee
        },
        "callback": function(response) {
             var employee = response.message;
             if (employee) {
                  frm.set_value('unit', employee.branch);
                  frm.set_value('department', employee.department);
             } else {
                  frappe.msgprint("Employee not found");
            }
        }
    }); 
})
1 Like

Thank you so much @NMyshuk its working,
also thanks to @lasalesi.