Assign new user to document

i want to assign new user to issue document based on some condition , and i want this user to appear on form sidebar under assigned to.

frappe.ui.form.on(‘Issue’, {
refresh: function(frm) {
frappe.db.get_value(‘Issue’, frm.doc.name,‘_assign’, function(value) {
if (value._assign.includes(‘user2’)){
let assignArray = JSON.parse(value[“_assign”]);
assignArray.push(“user3”);
console.log(“assignArray” + assignArray);
let newAssignString = JSON.stringify(assignArray);
value[“_assign”] = newAssignString;
} else {
console.log(“Not OK”);
}
});
}
});
the result is [“user1”,“user2”,“user3”]
but user3 did not appear on
Screenshot 2023-06-13 000113

i need help please .

maybe just need to refresh the page?

Hi:

Maybe you need more flexibility … but are you aware of this?
https://docs.erpnext.com/docs/v14/user/manual/en/automation/assignment-rule

Hope this helps.

yes i am aware of this , my problem with assignment rule is that i can not assign two people on the same document …

if there is a solution for this problem i would not get into coding :slight_smile:

Hi:

Assignment rule,as far I know, assign just 1 user …

Try this in your client script / .js code:

frm.assign_to.add()
frm.assign_to.assign_to.dialog.fields_dict.assign_to.set_value( ["user1@yourdomain.com", "user2@yourdomain.com"])
frm.assign_to.assign_to.dialog.fields_dict.description.set_value("Your description ")
frm.assign_to.assign_to.dialog.fields_dict.priority.set_value("High")
frm.assign_to.assign_to.dialog.refresh()
frm.assign_to.assign_to.dialog.primary_action()

Hope this helps.

It works,sets the user in assigned_to field but does not add, so the dialog box is still open, and have to manually click on add. Also, it shows fields have missing values: Assign To.

Found Solution. Changed the code a bit.

frm.assign_to.add()
frm.assign_to.assign_to.dialog.fields_dict.assign_to.set_value(["test@gmail.com","test2@gmail.com"])
setTimeout(function() {
         frm.assign_to.assign_to.dialog.refresh();
         frm.assign_to.assign_to.dialog.primary_action();
},2);

Hi @Nirvisha_Soni @avc While assigning user How can I give write permission?

Hi @Robot,

When you click “Assign,” a dialog opens up, but there’s no option to set conditions or read/write options. so you can’t configure this using client scripts.

Thank You!

Hi @NCP

Thank you for the response,
If we can’t configure using client scripts, is there a chance to do it using server scripts?

It might be achievable through server scripting, but you have to explore the core functionality.

1 Like

Hello, can you elaborate the problem? what do you want to achieve?

Hi @Nirvisha_Soni, It was resolved.
Previously I didn’t give user role properly so for the assigned user, task became read only, Now I gave project user role so, everything works fine
Thanks

BTW, I have one doubt. While creating task from server script using get_doc, Can I assign user? I’ve tried but it isn’t working tho

Yes, I have assigned the sales order. I used the To-Do feature for this task.

so = frappe.get_doc(
            {
            "doctype": "ToDo",
            "allocated_to": user_id,
            "reference_type": "Sales Order",
            "reference_name": doc.name(sales order id),
            "description": "Sales Order Assignment.",
            "status": "Open",
            "assigned_by": frappe.session.user,
            }
            ).insert(ignore_permissions=True)
1 Like

I mean, I’m asking about this assigned_to