How to assign a document to a user

how to assign a document to user not from frontend, not using assignment rule as I have to apply some logic to find the user to whom I need to assign the document, I want it from server side script

  1. Create a server-side script, for example, in the “YourDocType” DocType, under “Custom Scripts > Server Script”. This script will be executed when the document is saved.
  2. In the script, you can use the following Python code to assign the document to the desired user:
# Import necessary modules
from frappe.model.document import Document

# Function to assign document to user
def assign_document_to_user(doc, method):
    # Specify the desired username
    desired_user = "desired_user"

    # Update the owner field of the document with the desired user
    frappe.db.set_value("YourDocType", doc.name, "owner", desired_user)

# Attach the function to the before_save event of the document
YourDocType_before_save = {
    "before_save": "your_module.your_script.assign_document_to_user"
}

Save the script, and now, whenever a document of “YourDocType” is saved, it will be assigned to the specified user.

No , it will not assign the user in the assigned To list in the left side of the form.

Can you tell, how to assign the document to one or more users by Python code? I have the same problem. And there is no documentation about this topic…

first import “from frappe.desk.form.assign_to import add” in your python file and then use the below code
args = {
‘assign_to’ : [employee.email],
‘doctype’ : “Issue”,
‘name’ : self.name,
‘description’ : ‘test’,

	}

add(args, ignore_permissions=True)

where assign_to : is the user email to which you want to assign the document
doctype: name of doctype
name: Document to which you want to assign a user

1 Like

Thank you very much, this solved my problem. :heartbeat: