How to Fetch User Details like Name, Role, and Designation in Frappe
Fetch User Name and RoleLearn to retrieve user information in Frappe server scripts using frappe.db.get_value and frappe.get_roles. Covers session user, specific users, and related DocType fields.
In Frappe and ERPNext development, accessing user information is a fundamental requirement for customizing business logic, managing permissions, and creating personalized user experiences. Whether you need to know who is performing an action, what their roles are, or retrieve details from their Employee profile, Frappe provides a straightforward API. This guide breaks down the essential methods for fetching various user details in your server-side scripts.
1
2 # 1. Get the full name of the currently logged-in user 3 session_user_fullname = frappe.db.get_value("User", {"name": frappe.session.user}, "full_name") 4
5 # 2. Get the roles of the currently logged-in user 6 session_user_roles = frappe.get_roles(frappe.session.user) 7
8 # 3. Get the full name of a specific user (e.g., a user with the name 'manager@example.com') 9 manager_name = frappe.db.get_value("User", {"name": "manager@example.com"}, "full_name") 10
11 # 4. Get the associated Employee ID for the current user 12 # This assumes a link between the User and Employee DocTypes via the 'user_id' field in Employee 13 employee_id = frappe.db.get_value("Employee", {"user_id": frappe.session.user}, "name") 14
15 # 5. Get the designation of the user who last modified the current document ('doc') 16 # Useful in server scripts triggered on document events (e.g., on_update) 17 if doc.modified_by: 18 modifier_designation = frappe.db.get_value("Employee", {"user_id": doc.modified_by}, "designation") 19
20
Understanding This Code
What It Does
This collection of snippets demonstrates various methods to retrieve user-specific data such as full name, roles, and related Employee details within a Frappe server-side script.
When To Use
Use these methods in Server Scripts, Python hooks (e.g., 'on_update', 'validate'), or custom API endpoints whenever you need to access information about the current user or other users in the system.
Prerequisites
- •Basic understanding of Frappe Server Scripts.
- •Familiarity with the User and Employee DocType structures.
Key Concepts
Important ideas to understand in this code
frappe.session.user
A global variable in Frappe that holds the 'name' (typically the email address) of the user who is currently logged in and making the request. It is the primary identifier for the session user.
Learn morefrappe.db.get_value
An efficient database API method to fetch a single field value from a specific DocType. It takes the DocType name, a dictionary of filters, and the field name you want to retrieve as arguments.
Learn morefrappe.get_roles
A utility function that returns a list of all roles assigned to a specified user. If no user is provided, it defaults to the current session user.
Learn moreStep-by-Step Tutorial
Follow along to understand how this code works
Fetch the Current User's Full Name
This is the most common use case. We use `frappe.session.user` to identify the current user and pass it as a filter to `frappe.db.get_value` to retrieve the 'full_name' field from their User document.
current_user_name = frappe.db.get_value("User", {"name": frappe.session.user}, "full_name")
frappe.msgprint(f"Hello, {current_user_name}")Retrieve the Current User's Roles
The `frappe.get_roles` function directly returns a Python list of all roles assigned to the user specified. This is useful for implementing role-based logic in your scripts.
user_roles = frappe.get_roles(frappe.session.user)
if "System Manager" in user_roles:
# Execute logic for System Managers
passGet Information from a Linked DocType
Often, user details are stored in the Employee DocType, which is linked to the User DocType. To fetch a field like 'designation', you must query the 'Employee' DocType, filtering by the 'user_id' field.
designation = frappe.db.get_value("Employee", {"user_id": frappe.session.user}, "designation")Fetch Details of Another User
You can fetch data for any user, not just the session user. In this example, we fetch the designation of the user who last modified the current document (`doc`), which is available in document-triggered server scripts.
# This code runs inside a script triggered by a document event
last_modifier = doc.modified_by
modifier_designation = frappe.db.get_value("Employee", { "user_id": last_modifier }, "designation")Common Issues & Solutions
Troubleshoot problems you might encounter