Sumbitted forms in list view only visible based on username present in field

Hi, I’m very new to Frappe and ERPNext.
I have a very specific thing that I want to do to my application.

Imagine a user named “John D” is currently logged in.
In the doctype listview, only the submitted forms that have the field called “example_field” and the value is ‘John D’ should be viewable to John.

If any other user is logged in, they should only be able to view the submitted forms that have their names in the “example_field” field. Thus, the user can see all submitted forms with their respective username present in a specific field.
This should be by default, without manual filters which can be disabled by the user.

Is this possible? If so, how do i start customizing frappe?

Regards,
-Akhil

1 Like

Hi,
I have a question are these documents submitted by them?

if they are why not use workflows…Here the documentation link>>>https://docs.erpnext.com/docs/user/manual/en/workflows

Also there is field called ‘owner’ for the document. you can use that to identify who created this document or use there should be field last_updated_by (I am not sure about this but there should be something like that) that you can use to setup a workflow.

No, the documents are not submitted by the respective user who should be able to view the documents

We also faced this issue. There are two ways to do this:

  1. You can use a client script to change the ownership of the doctype so that you can use the permission manager to give only the permissions to the records that are owned by the user.

  2. The second way is to use the “shared with” option in the specific record. Here you can select a user and give them permissions for the specific record. I have not looked at to what extend that can be automated.

Tried user permissions? This is exactly what it does and it can’t be disabled by the user.

https://docs.erpnext.com/docs/user/manual/en/user-permissions

If you need even more control then you will have to write a permission query: Hooks

1 Like

I see, an ownership change for each instance of records in the doctype if possible would be a good solution, i’ll look into it

Can user permissions be set automatically if a new user logs in?
What I was looking for is when a new user logs in, they would be able to view all the forms that have their respective name in a field in the doctype list view. Is this something that can be done with permission query / hooks?

To achieve this in Frappe/ERPNext, you can utilize either permission manager and custom scripts. Here’s how you can approach it:

  1. Permission Manager:
    Set up permissions so that users can only view documents where their username matches the value in the “example_field” field. This can be done by configuring roles and permissions in the Role Permission Manager.

  2. Custom Script:
    Write a custom server script to fetch documents where the “example_field” contains the username of the logged-in user. This script would filter out documents based on the user’s username and the value in the “example_field”.

  3. Client-Side Scripting:
    Implement client-side scripts to ensure that only the relevant documents are displayed in the list view. This script should trigger when the list view is loaded, calling the custom server script to fetch and display the filtered documents.

Here’s a basic outline of how you can implement this:

# Custom Server Script
# Fetch documents where example_field matches the logged-in user's username
@frappe.whitelist()
def get_filtered_documents(user):
    documents = frappe.get_all("YourDocType", filters={"example_field": user})
    return documents

# Client-Side Script
# Call the server script to fetch filtered documents when the list view is loaded
frappe.listview_settings['YourDocType'] = {
    onload: function(list_view) {
        frappe.call({
            method: 'your_app.module_name.doctype_name.get_filtered_documents',
            args: { user: frappe.session.user },
            callback: function(response) {
                // Process the response and display filtered documents in the list view
            }
        });
    }
};
1 Like