Get user name of logged in user and write to new record

Hello all,

I’m convinced that ERPNext is what we need for a prefecture-wide healthcare program we are attempting to manage more efficiently but I keep running into little hurdles here and there. As such, I’m unintentionally testing the forums whilst very intentionally testing ERPNext. :slight_smile:

Anyway, sorry for the blather…I want to fetch the current logged in user (the user adding a record…in this case who is adding a patient) and write his/her name (in this case an institution name) with the patient record. The idea behind this is that I want to know who (what institution) created what patients for later tracking.

I managed to retrieve the logged in user with the following:

Using a link field to the “User” doctype and with a default of user_fullname as explained here

The problem is that when I try to save a new patient using this field I get an error saying “Could not find ###” with the name of the user that is being displayed. Note ### is a renamed text value in Japanese for “User”. If I had not renamed the display name the error would read “Could not find user: My Name”.

It seems ad odd error because the system clearly found the user because it is displayed as the default value. It is only when I try to save the record (create the patient) that it throws a fit.

Any advice will be most appreciated.

Jason

we have similar requirement.

If you are using Link field to User doctype, It must always save the value of name field.

use user.name to save to link field.
add 1 more field called user_full_name, and save User.full_name there

revant_one,

Thank you very much for this advice. I think I’m following the logic here but I’m a bit confused as to where to add the additional field…? I can’t add a field to the User doctype, can I? I get the message “Core DocTypes cannot be customized” when I try to do so with the User doctype. If I use user.name as the default with a link to the User doctype my field is simply filled with “user.name”.

I’m probably missing something very simple here.

With my initial attempt, I was able to get the field populated with the logged in user but it won’t allow me to save.

Any additional details you can provide will hopefully get me going down the correct path. :slight_smile:

Cheers
Jason

P.S. Should the additional field be in my “Patient” doctype? And, if so, should it be a Link field? Just experimenting here a bit and not making any progress…aside from being able to refine my questions. :slight_smile:

revant_one,

Thanks again for your advice. I wasn’t able to get the solution you provided to work but…it did help me to stumble onto what I think is a solution. Instead of using the “Link” field I used “Read Only” with the same settings (i.e. connected to the User doctype and with the default of user_fullname). This populates the field with the users name and, to my surprise, can be saved. :slight_smile: I was worried that it would simply display the name of whoever is logged in but that does not seem to be the case. The name of the original record creator (for patient in our case) stays in the field and can be viewed even when logging in with a different account. So…for now, this works for me. :slight_smile:

Thanks again

Jason

1 Like

my final solution for print owner user name and submit user name is as following

  1. use monkey patch in custom app to add 2 methods to BaseDocument, for specific doctype, you can add these 2 methods to your doctype class also
    (refer to this app for how to monkey patch in custom app GitHub - aakvatech/CSF_TZ)

  1. in custom print html field, add the own user name and submit user name field as following

{{ doc.get_owner_username() }} {{ doc.get_submit_username() }}