Client Script select the employee from frappe.session.user

Hi,
I’m working on a Client script that will automatically select the right employee from the frappe.session.user.

I tried different things but I’m not sure what is the right way to do that.

I think I need to make a frappe.call to get employee list and then filter with the user_id which is the email login.

But to make a frappe.get_list I need to whitelist and I’m a bit lost with that.

Is there another simple way to do that in the client script?
I mean I could do a simple switch statement and manually setting the employee but it’s not a really nice way to do it…

Thanks!

Hi @Samuel_Gervais,

Get Employee ID for please apply custom/client script for it.

frappe.db.get_value("Employee", {
    "user_id": frappe.session.user
}, ['name'],
function (value) {
    console.log("Employee ID", value.name);
});

Please check in the console if the user id is added to the employee master.

Thank You!

2 Likes

Thank you for the quick reply! It works perfectly! :slight_smile:

Hi @NCP

I have created a custom data field type on the DocType Quotation “full_name” to store the employee name who created the quotation.
Then I created a client-side script and a variable on my on this script called value_name to store the employee name from frappe.db.get_value and set the value of this variable in the field full_name as shown:

frappe.ui.form.on(‘Quotation’, {
refresh(frm) {
// your code here
var value_name = frappe.db.get_value(“Employee”, {“user_id”: frappe.session.user}, [‘employee_name’]);

cur_frm.set_value(‘full_name’, value_name);
}
})

but the output I am getting this instead of the employee name:
image

could I get maybe more help on this?

Thanks a lot!

Hi @mr-elamin,

Please apply it.

frappe.ui.form.on('Quotation', {
    refresh(frm) {
        // your code here
        frappe.db.get_value("Employee", {"user_id": frappe.session.user}, ["employee_name"])
            .then(response => {
                var value_name = response.message.employee_name;
                console.log(value_name); // Check if value_name is fetched correctly
                cur_frm.set_value('full_name', value_name);
            })
            .catch(err => {
                console.log(err);
            });
    }
});

I hope this helps.

Thank You!

1 Like