Try ERPNext Buy Support Partners Foundation Foundation Members

Create new record automatically

Hi guys, im trying to add new record automatically in Doctype B before i save Doctype A but im getting allways the same error.

ERROR:

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 61, in application
response = frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 56, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 1036, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/uni3/doctype/doctypeA/doctypeA.py”, line 28, in create_test
NameError: name ‘cur_frm’ is not defined

Python :

class Doctype A(Document):
pass
@frappe.whitelist()
def create_test():
client = “Test”;
doc = frappe.get_doc({
“doctype”: “Doctype B”,
“employee”: “Test”,
“employee_company”: client
})
doc.insert(ignore_permissions = True)

Javascript

frappe.ui.form.on(“Doctype A”, {
after_save: function (frm) {
var cliente = frm.doc.fetch_cliente;
frappe.msgprint(“START”);
frappe.call({
method: ‘erpnext.uni3.doctype.licencas.licencas.create_test’,
args: {
employee_company: cliente
},
callback: function® {
if(r.message) {
frappe.msgprint(“Works”);
}
}
});
}
});

Im new to python and erpnext so it may be something really stupid that im missing and i cant figure it out.

cur_frm is used in JS which denotes to current form instance.
What’s statements are there in create_test method ? Check line#28

doctypeA/doctypeA.py”, line 28, in create_test
NameError: name ‘cur_frm’ is not defined

the thing is in the .py file doesnt even have 28 lines so i dont get the error

Python print screen

Javascript Print screen

In the post i change some values cause i use portuguese and i changed it to english so everyone understand

But what im trying to do is from “Licencas” after save create a new “funcionario” automatically

You pass functionario_empresa parameter to the frappe.call() but to not receive any parameters on the Python side. Add a parameter with the same name to the Python function. You will also need to frappe.parse_json() on the parameter, to extract the serialised data:

@frappe.whitelist()
def create_functionario(functionario_empresa):
    fe = frappe.parse_json(functionario_empresa)
    cliente = fe['cliente']
    <use cliente>

I was trying what u said but until now with no success, doesnt give me any error but doesnt create the record in the doctype either

Try to frappe.msgprint() the contents of the passed variable on the Python side (the print function works exactly the same as on the JS side) to see how to handle it. I don’t remember right away how the API system handles serialized parameters. I think some of the data types get converted on the fly.

frappe.msgprint(str(functionario_empresa))
frappe.msgprint(str(type(functionario_empresa)))

when i call the py function in the console and put the args by hand, it adds a new record in the doctype

frappe.msgprint(str(functionario_empresa))
frappe.msgprint(str(type(functionario_empresa)))

the frappe.msgprint i couldnt make it work at least it didnt print nothing

I think the problemis in the .js frappe call but i cant figure what it is

Make an early return 'test' from the Python function and try accessing the method like this:

http://localhost:8000/api/method/dottet.path.to.method.file.and.test_api_method

You should get something like this:

image

I think you might have the dotted path wrong.

Im going to try to explain better how the doctypes are working so it may help in the resolution.

Doctype A (Licencas) has a link field to Doctype C (cliente) called “fetch_cliente”
Doctype B (Funcionarios) has a link field to Doctype C (Cliente) called “fetch_cliente”

And what i want is:

On save of doctype A create a new record in Doctype B “Funcionarios”

My python code is:

Python file:

def create_funcionario(fetch_cliente):
fe = frappe.parse_json(fetch_cliente)
cliente = fe[‘cliente’]
print(“test”)
doc = frappe.get_doc({
“doctype”: “Funcionarios”,
“funcionario_nome”: “Teste”,
“fetch_cliente”: cliente
})
frappe.msgprint(str(fetch_cliente))
frappe.msgprint(str(type(fetch_cliente)))
doc.insert(ignore_permissions = True)

Javascript:

frappe.ui.form.on(“Licencas”, {
before_save: function (frm) {
frappe.msgprint(“START”)
frappe.call({
method: ‘erpnext.uni3.doctype.licencas.licencas.create_funcionario’,
args: {
fetch_cliente: fetch_cliente
},
callback: function® {
if(r.message) {
frappe.msgprint(“Worked”);
}
}
});
}
});

If i do "bench execute erpnext.uni3.doctype.licencas.licencas.create_funcionario --args “Teste” the record is created so i think the python is working.

when i try in browser

http://localhost:8000/api/method/erpnext.uni3.doctype.licencas.licencas.create_funcionario

gives me the following error

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 66, in application
response = frappe.api.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/api.py”, line 56, in handle
return frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 56, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 1036, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/uni3/doctype/licencas/licencas.py”, line 28, in create_funcionario
NameError: name ‘cur_frm’ is not defined

Please, show the full licencas.py file. Python side should not know anything about the cur_frm.

In this image u can even see the path for the file

btw thank you for taking ur time to help me

I should have asked this earlier, how did you create the uni3 directory?

i created it in the erpnext backoffice (configuration tab)and then add it to the desktop.py

i run bench migrate after

im running erpnext V11

Okay, everything looks correct.

I’ve just tried to create a new API method for a new DocType in my app’s module:

/home/frappe/frappe-bench/apps/dc_plc/dc_plc/dc_documents/doctype/dc_doc_desdoc_meta/dc_doc_desdoc_meta.py:

from __future__ import unicode_literals
from frappe.model.document import Document
import frappe

class DC_Doc_Desdoc_Meta(Document):
	pass


@frappe.whitelist()
def test_method(test_param='test_data'):
	return 'test_response'

Then I tried to access it via the browser:

http://localhost:8000/api/method/dc_plc.dc_documents.doctype.dc_doc_desdoc_meta.dc_doc_desdoc_meta.test_method

and got the expected response:

image

Try creating similar simple test api method and access it from the browser. Once you get a correct response, you can build on that, starting with using the JS to poke that method.

I tried this is the same file the “licencas.py”

and gave me the same error from before

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 66, in application
response = frappe.api.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/api.py”, line 56, in handle
return frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 45, in execute_cmd
raise e
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 42, in execute_cmd
method = get_attr(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 135, in get_attr
method = frappe.get_attr(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 1027, in get_attr
return getattr(get_module(modulename), methodname)
AttributeError: module ‘erpnext.uni3.doctype.licencas.licencas’ has no attribute ‘test_method’

now im gonna try create a new doctype and test it

So i created a new doctype in the CRM module from erpnext named it “dc_doc_desdoc_meta”
i copy the method as u can see below

then i tried to call it in the console/terminal

terminal

then i tried to call it in the browser and gave me the same error as before

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 66, in application
response = frappe.api.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/api.py”, line 56, in handle
return frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 45, in execute_cmd
raise e
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 42, in execute_cmd
method = get_attr(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 135, in get_attr
method = frappe.get_attr(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 1027, in get_attr
return getattr(get_module(modulename), methodname)
AttributeError: module ‘erpnext.crm.doctype.dc_doc_desdoc_meta.dc_doc_desdoc_meta’ has no attribute ‘test_method’

Sorry, I ran out of ideas for now and will have to research this matter deeper.

Let’s hope someone more proficient than me will show up :grinning:

1 Like

thank you anyway for the help really appreciate it :smiley:

Can it be something about my module , i was trying around and i put the function test_method in customer doctype

http://localhost/api/method/erpnext.selling.doctype.customer.customer.test_method

And it worked

test

But in my licencas.py

keeps giving me the same error as before, now im very confused