Assign a document using a custom script or python code

Hi Everyone,

I am looking for a way to assign a document using a javascript code.
Is is possible through javascript code.
If yes, then can anybody help me in this regard.

If no, then can anybody tell me how to run this code:

frappe.db.set_value(doctype,name,“assigned_to”,"ruchin.sharma@abc.com");
Or is there any other way to do it?

You can use the set_value API

https://docs.frappe.io/current/api/frappe.client#frappe.client.set_value

Hello Mr. Mehta,
I appreciate, if you can elaborate the same, actually I am very new to this technology.
Also, I really don’t know om which event I should use it and what exactly is the field name of assigned to.

Is it assigned_to or assign_to

I tried using:
cur_frm.set_value(doctype,name,“assigned_to”,"ruchin.sharma@xyz.com");
cur_frm.set_value(doctype,name,“description”,"ruchin.sharma@xyz.com");

AND

cur_frm.set_value(doctype,name,“assign_to”,"ruchin.sharma@misport.com");
cur_frm.set_value(doctype,name,“description”,"ruchin.sharma@misport.com");

both didn’t work.

Please help

@ruchin78 use a custom script, is is easy!

 cur_frm.assign_to.add();
 cur_frm.assign_to.dialog.hide();
 cur_frm.assign_to.dialog.set_values({field:"value"});
 cur_frm.assign_to.add_assignment();

For you see which values you can use check this strech of code https://github.com/frappe/frappe/blob/develop/frappe/public/js/frappe/form/footer/assign_to.js#L95

1 Like

Hi Max,

Thanks for the same it really worked for me, the only problem I am facing is if the document is already assigned to someone, it does a new entry for the same and there remains both the assignments therefore I was looking for the way to remove the existing assignment before assigning it.

@ruchin78, add this at the begin of the last code

cur_frm.assign_to.remove(cur_frm.doc.owner)

cur_frm.assign_to.add_assignment();
didn’t work for me
i used frm.assign_to.dialog.primary_action(); insted

@max_morais_dmm
@yohendry_hurtado

Below is my code however it is working fine but doesn’t work well if there is already an assignment

frappe.ui.form.on("Purchase Order", "assign_the_approver", function(frm) {
     
     if(frm.get_docinfo().assignments.toString())
     {
        cur_frm.assign_to.remove(frm.get_docinfo().assignments[0].owner)
     }

cur_frm.assign_to.add();
cur_frm.assign_to.dialog.set_values({assign_to:frm.doc.financial_approver});
cur_frm.assign_to.dialog.set_values({description:"Urgent"});
cur_frm.assign_to.dialog.hide();
//cur_frm.assign_to.add_assignment();
frm.assign_to.dialog.primary_action();

cur_frm.set_df_property("assign_the_approver","hidden",true);
});

Error Message:
Error: Document has been modified after you have opened it (2016-03-17 23:11:24.387348, 2016-03-17 23:11:24.682756). Please refresh to get the latest document.

and after the message there will be no assignment.

1 Like