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.
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?
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.
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.
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")
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)
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)