Auto fetch User ID and pass it into Table

Hi Everyone,

I’ve created a table inside the User Doctype that links to other doctypes (not a child table) because I’ll need to see the reports of the data inserted into this table later.

Actually, I have two questions, the first field in this table is a link to “User” Doctype, which my intention is to select the proper user to fill up the information, but I’ve noticed that only the System users are listed and not the Website Users. Is there any way that I can display the rest of the users here? Does the website user is only associated with client records?


My second question is about the same first field in the table (user field) is there any possibility where I can make a custom script to fetch the data from the user that I’m editing and pass the data to the field (automatically fill the user field in the table)?

I’ve been through some tutorials and I’m still learning as much as I can, in the “Client Side Scripting Index” shows how to “Fetching a table row’s values in the form” could I use a similar way but to achieve that?

Could someone please give me a hint on this?

Thank you!!

Regards,
João

Hi @Joao_Carvalho,

for your first question on a filter for desk user only, I cannot reproduce this behaviour, I get all active users. Do you maybe have them disabled?

As for the second part, to fetch related data, it is probably not even required to go through a client-side script. Have a look at the Sales Invoice DocType: it has a link field to Customer and looks up the customer name automatically


This works when the link field is a DocType and you want to find an attribute of it.

If you really want to fetch something, you can do this (see https://erpnext.org/docs/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples/custom-script-fetch-values-from-master)

add_fetch(link_fieldname, source_fieldname, target_fieldname)

but make sure that the DocType to be fetched from is cached, otherwise it will not work. In that case you will have to use a full db lookup, see Delivery note customization - #2 by lasalesi

Hope this helps.

1 Like

Hi @lasalesi

Thanks so much for your help!

Humm seems strange indeed, I get only the desk/system users and not the others, I’ve all the users activated/enabled though.
Do you think it could be because I’m still using the ERPnext/Frappe 9.2.15 version, I haven’t updated it yet because I’ll probably lose some tweaks in some python files in order to suit my needs. After that, I’ll definitely push/commit the changes to git.

That’s great, I can work around using a similar approach like the one you described.
But that means that I need to use a “Link” field first and the info is auto-completed on the other field right?
My idea is something like that: I’m editing a user and I filled up a table related to the other doctype. when I add a new row can I use a custom script to fetch the data automatically from the user that I’m editing?

This is what actually happens:

table_without_default

I’ve tried to set the “user” option in the default field and using this option the new rows will automatically fill up with the current user logged in, in my case me.

That is what it looks like after setting the default field.

table_with_default

What I’m actually trying to achieve is automatically fill up each row with the user that I’m editing instead of the one that’s logged in.

Do you know if I can set another argument instead of “user” in the default options to get what I’m looking for? or can I achieve that with a “add_fetch”? Will that work even If a using a table (Doctype) and not a Child table?

Many thanks!

Regards,
João

Hi @lasalesi,

ok, understand your usecase. So you have one field, e.g. “user” in the parent DocType that contains the user name (Link field). You then have a subtable, I’ll call it user of type “User Entry”, in which each line should contain the user. Try this custom script (on the parent DocType, but with the on(“child doctype”)):

frappe.ui.form.on("User Entry", {
  users_add: function(frm) {
	// this function is called when the value of customer is changed.
        var last_id = frm.doc.users.length - 1;
	frappe.model.set_value(frm.doc.users[last_id].doctype, frm.doc.users[last_id].name, "user", frm.doc.user);
  }
}); 

In this example, the parent DocType has a field “user” (type Link) which contains the user. It has a subtable of DocType “User Entry”, which has the name “users” that also has a field user. On the customs cript of the parent DocType, the above code is located. Whenever a line is added, the parent user is fetched into the user field of the row item.

As for the filter, this behabiour has to my knowledge not changed since v9. Maybe you can check if there is a dict filter applied?

// filter 
cur_frm.fields_dict['User'].get_query = function(doc) {
    return {
        filters: {
               "disabled": 0
        }
    }
}

Hope this helps.

1 Like

Hi @lasalesi

Thank you so much for your response and time helping me out!

Works everything like a charm!

All the best 2 you!
João

I have same requirement where I have table in “Customer” doctype as parent and “Customer Items” is child. like your requirement you need user in table i want customer name in table
please suggest the script. please add your final working script.

thank you

thank you