Help needed on Frappe.Call method

I am using the below code on the customized button on purchase order just for testing purpose.
Actually, I want to call item document with some parameters to create a new document.
But then moment I press this button on the Purchase Order, it says “No Permission”
I appreciate, if anybody can help me in this regard.

Because I will use the same concept to generate a new item code in Item Document.

cur_frm.cscript.update_qty = function() {
return frappe.call({
method: “erpnext.stock.doctype.item.create_a_new_item”,
args: {
“Item”: cur_frm.doc.name
},
callback: function® {
var doclist = frappe.model.sync(r.message);
frappe.set_route(“Form”, doclist[0].doctype, doclist[0].name);
}
});
}

1 Like

Add create_a_new_item method, under frappe.whitelist().

frappe.whitelist() def create_a_new_item(): """ your code """

Hi Saurabh,
Thanks a lot for your information but tell me one thing, how to call a different form/document in new document mode for generating a new document?

Regards
Ruchin Sharma

Hi Saurabh,
I have created a function for the same, now can you please tell me how to return a value from the function to the script.

from function return a value and capture that in callback()

Python code
frappe.whitelist() def create_a_new_item(): """ your code """ return values_want_to_return

js code
frappe.call({ method:method, args: args, callback:function(r){ r.message.values_want_to_return} })

2 Likes

I appreciate if you can give an example for the same.
Below is my code:

@frappe.whitelist(allow_guest=True)
def generate_document(source_name, target=None):
doc_m = frappe.get_doc(“Item”, source_name)
i=doc_m.item_group[:3]+’%’
j=frappe.db.sql(""“select item_code from tabItem where item_code like %s order by item_code desc limit 1"”",i)
prefix=j[0][0]
prefix="".join(prefix)
new_prefix=prefix[0:3]
new_suffix=prefix[4:10]
next_num=(int(new_suffix)+1)
next_num1=str(next_num)
new_num1=next_num1.decode(“ascii”).rjust(6,‘0’)
new_num1=new_prefix+’-’+new_num1
new_item=frappe.new_doc(“Item”)
new_item.item_code =new_num1
new_item.item_group = doc_m.item_group
new_item.item_name = doc_m.item_code+" Insert New Name"
new_item.stock_uom =doc_m.stock_uom
new_item.description = “Insert New Description Here”
new_item.default_warehouse = doc_m.default_warehouse
new_item.insert()
frappe.msgprint(“New Item Code “+new_num1+” has been generated”)

now tell me one thing
if I do write a return statement at the end of this code say
return new_num1

Then where I will store the same in other side in my JS code
Also, I am facing one more issue in it, that is when I try to run this code on a new document it says there are unsaved changes in the document something like this.

Actually I want to generate the maximum part code in the selected item_group series and tell the user that this is the number you need to assign to this part code. But it works only on the already saved document or either I need to save the document first.
Can you tell me a workaround for it.

My JS Code:

frappe.ui.form.on("Item", "generate_code", function(frm, doctype, name) {
frappe.call({
"method": "library.item_script.generate_document",
args: {
doctype: "Item",
item_group: frm.doc.item_group
},
callback:function(r){
cur_frm.set_value("item_code",new_num1);}
});
cur_frm.set_df_property("generate_code","hidden",true);
});

It is written on a custom button called generate code on item doctype

My Python Code:

@frappe.whitelist(allow_guest=True)
def generate_document(source_name, target=None):
doc_m = frappe.get_doc("Item", source_name)
i=doc_m.item_group[:3]+'%'
j=frappe.db.sql("""select item_code from tabItem where item_code like %s order by item_code desc limit 1""",i)
prefix=j[0][0]
prefix="".join(prefix)
new_prefix=prefix[0:3]
new_suffix=prefix[4:10]
next_num=(int(new_suffix)+1)
next_num1=str(next_num)
new_num1=next_num1.decode("ascii").rjust(6,'0')
new_num1=new_prefix+'-'+new_num1
frappe.msgprint("New Item Code "+new_num1+" has been generated")
return new_num1;

But when I open a new item document and select item group and then press generate code button I receive the below error on my console and I don’t get any result.

SyntaxError: unreachable code after return statement desk.min.js:73:12432
SyntaxError: unreachable code after return statement desk.min.js:7992:187
localStorage cleared desk.min.js:7181:1
Cleared App Cache. desk.min.js:7178:237
localStorage cleared desk.min.js:7181:1
TypeError: ‘submit’ called on an object that does not implement interface HTMLFormElement. jquery.min.js:4:12965
Use of getPreventDefault() is deprecated. Use defaultPrevented instead.

Can anybody help me

  1. Please format your code if you want help
  2. Seems like the button is not triggered, or your code is not loaded. How are you loading ti?
  1. You need to take some time to answer some questions too. This is a community forum and help has to be two-way

Thanks Mr. Rushabh Mehta,
Here is my code with formatting:
JS Code:

frappe.ui.form.on(“Item”, “generate_code”, function(frm, doctype, name) {

frappe.call({
				"method": "library.item_script.generate_document",

				args: { doctype: "Item", item_group: frm.doc.item_group },
				
				callback:function(r){ cur_frm.set_value("item_code",new_num1);}
			});

cur_frm.set_df_property("generate_code","hidden",true);

});

================================================
PY Code:

@frappe.whitelist(allow_guest=True)
def generate_document(source_name, target=None):

doc_m = frappe.get_doc("Item", source_name)

i=doc_m.item_group[:3]+'%'

j=frappe.db.sql("""select item_code from tabItem where item_code like %s order by item_code desc limit 1""",i)

prefix=j[0][0]

prefix="".join(prefix)

new_prefix=prefix[0:3]

new_suffix=prefix[4:10]

next_num=(int(new_suffix)+1)

next_num1=str(next_num)

new_num1=next_num1.decode("ascii").rjust(6,'0')

new_num1=new_prefix+'-'+new_num1

frappe.msgprint("New Item Code "+new_num1+" has been generated")

return new_num1;

I appreciate, if you can help me now.

Can anybody help me on this?

Please check output of [quote=“ruchin78, post:9, topic:7089”]
source_name
[/quote]

in python code.

It gives an error when I press the generate_code button
Traceback (innermost last):
File “/home/sam/frappe-bench/apps/frappe/frappe/app.py”, line 67, in application
response = frappe.handler.handle()
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 74, in handle
execute_cmd(cmd)
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 99, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/sam/frappe-bench/apps/frappe/frappe/init.py”, line 758, in call
return fn(*args, **newargs)
TypeError: assign_user() takes at least 1 argument (0 given)

@ruchin78 I think you have posted wrong log. This log is related to Assign a document using a custom script or python code post.

Ah! So, sorry for this, the right log is:

Traceback (innermost last):
File “/home/sam/frappe-bench/apps/frappe/frappe/app.py”, line 67, in application
response = frappe.handler.handle()
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 74, in handle
execute_cmd(cmd)
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 99, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/sam/frappe-bench/apps/frappe/frappe/init.py”, line 758, in call
return fn(*args, **newargs)
TypeError: generate_document() takes at least 1 argument (0 given)

In frappe.call() you are passing two arguments doctype and item_group

in handler method, you have missed parameters.

rewrite method, in a way that it will accept arguments: doctype and item_group,

your method should be,

def generate_document(doctype, item_group=None):

It worked for me, thanks a lot.