Just want to write a simple Server script to call a function

I have a workflow setup in Expense Claim, i want that once the document is submitted, this document is shared with a particular user (fixed for all)

I used below server scripts, which didn’t worked, throws issues like “import not found”, “add function not found”, or simply doesn’t work

A) didn’t worked

add(doc.doctype, doc.name, user='ayush@gmail.com', read=1, write=1, submit=1, share=1, everyone=0)

B)

from frappe.share import add
add(doc.doctype, doc.name, user='ayush@gmail.com', read=1, write=1, submit=1, share=1, everyone=0)

C)

def on_submit(self):
	frappe.share.add(
			self.doctype, self.name, self.assign_to, write=1, submit=1, share=1, flags={"ignore_share_permission": True}
		)

D)

import frappe

def on_submit(doc, method):
    if doc.workflow_state == 'Verification Pending':
        # Share the document with the specified user
        frappe.share.add(
            doctype=doc.doctype,
            name=doc.name,
            user='ayush@gmail.com',
            read=1,
            write=0,
            share=0,
            everyone=0
        )
        frappe.msgprint(f"Document shared wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwith ayush@gmail.com")

# Connect to the 'After Submit' event of the Expense Claim doctype
doc_events = {
    "Expense Claim": {
        "on_submit": on_submit
    }
}

Please anyone help, i don’t think it is so tough to write a Server script.

Hi @Rajat96318,

try using that style and ajust for your doctype. tested and worked for me.

# Define static task name and user
task_name = "TASK-2024-02568"
user = "email@user.com"

# Fetch the task document
task = frappe.get_doc("Task", task_name)

# Prepare the share data
share_data = {
    "doctype": "DocShare",
    "user": user,
    "share_doctype": "Task",
    "share_name": task.name,
    "read": 1,  # Allow read access
    "write": 0, # Disallow write access (set to 1 if write access is needed)
    "share": 0  # Allow sharing
}

# Create a new DocShare for sharing the task
new_share = frappe.get_doc(share_data).insert(ignore_permissions=True)

# Commit changes to the database
frappe.db.commit()

also do not use any frappe import etc. on server or client script when creating them on the GUI.

this error is coming

i modified your code according to my need

# Define the user to share the document with
user = "ayush@gmail.com"
task_name = "HR-EXP-2024-00152"

task = frappe.get_doc("Expense Claim", task_name)

# Function to be called on submission of Expense Claim
# def on_submit(doc, method):
    # Prepare the share data
share_data = {
    "doctype": "DocShare",
    "user": user,
    "share_doctype": "Expense Claim",
    "share_name": task.name,
    "read": 1,  # Allow read access
    "write": 0, # Disallow write access (set to 1 if write access is needed)
    "share": 0  # Allow sharing
}

    # Create a new DocShare for sharing the expense claim
new_share = frappe.get_doc(share_data).insert(ignore_permissions=True)

# Commit changes to the database
frappe.db.commit()

Please Help

@pronext Resolved.
This is the modified working code

user = "ayush@gmail.com"

task = frappe.get_doc("Expense Claim", doc.name)

# Function to be called on submission of Expense Claim
# def on_submit(doc, method):
    # Prepare the share data
if doc.workflow_state == "Verification Pending":
    share_data = {
        "doctype": "DocShare",
        "user": user,
        "share_doctype": "Expense Claim",
        "share_name": task.name,
        "read": 1,  # Allow read access
        "write": 0, # Disallow write access (set to 1 if write access is needed)
        "share": 0  # Allow sharing
    }

    # Create a new DocShare for sharing the expense claim
    new_share = frappe.get_doc(share_data).insert(ignore_permissions=True)


One Problem
when i change the action of workflow, and the docstatus chnage to submitted, it throws error of permission

The problem is, this the account manager person, so he should not see all employee data, i know when he has permission of employee he can do submit it,
but i don’t what him to see employee data

How can i do this, anyone please help

Seems to be a permission error.

You could do the sharing function in a server script on API Type and then call the method from the client script, following your logic.

This way no user permissins are requested as the automation is done by the Admin via server script.

Resolved, it was simple mistake in server script, didn’t given submit access
Updated working code

user = "ayush@gmail.com"
task = frappe.get_doc("Expense Claim", doc.name)

if doc.workflow_state == "Verification Pending":
    share_data = {
        "doctype": "DocShare",
        "user": user,
        "share_doctype": "Expense Claim",
        "share_name": task.name,
        "read": 1,  # Allow read access
        "write": 1, # Disallow write access (set to 1 if write access is needed)
        "submit": 1,
        "share": 0  # Allow sharing
    }

    # Create a new DocShare for sharing the expense claim
    new_share = frappe.get_doc(share_data).insert(ignore_permissions=True)

Thank you @pronext for your help