Whitelisted server script

Hey everybody,

Keep hitting a wall with this one:
I created an app. It has its own doctypes.
Now trying to call a whitelisted server script and I am probably doing something wrong, but can’t figure out what it is.
on the client script:

frappe.ui.form.on('Test DocType'{
	test_btn(frm) {
		var first_var = frm.doc.first;
		var second_var = frm.doc.second;
		frappe.call('test_whitelisted_function',{'first': first,'second': second})
			.then(r => {
				alert('Success!');
			});
	}
});

On the server, in: /home/frappe/frappe-bench/apps/test_app/test_app/test_app/doctype/test_doctype/test_doctype.py:

from frappe.model.document import Document
import frappe

class TestDocType(Document):

	@frappe.whitelist()
	def test_whitelisted_function(first = None, second = None):
		import requests
			return 'Something'

Yet, when clicking test_btn Button on the “Test DocType” form, I get the following:
Failed to get method for command test_whitelisted_function with 'test_whitelisted_function'.
Here is the site’s site_config.json:

{
 "db_name": "_somedbname",
 "db_password": "password1234",
 "db_type": "mariadb",
 "developer_mode": 1,
 "server_script_enabled": true
}

For the record:
I tried: bench update and bench clear-cache, rebooted both my machine and server, and ran out of ideas.
Please help!

Just to make sure I give the whole picture. I also tried:

from frappe.model.document import Document


class TestDocType(Document):
	pass

import frappe
@frappe.whitelist()
def test_whitelisted_function(first = None, second = None):
	import requests
	return 'Something'

and different kinds of:

frappe.call('test_app.test_app.doctype.test_doctype.test_doctype.test_whitelisted_function',{'first': first,'second': second})

still getting errors such as:

Failed to get method for command test_app.test_app.doctype.test_doctype.test_doctype.test_whitelisted_function with module 'test_app.test_app.doctype.test_doctype.test_doctype' has no attribute 'test_whitelisted_function'

And also tried putting the python script in a different file and pointing to it.

Write your function at the end of class and you must use @frappe.whitelist() like,

from frappe.model.document import Document
import frappe

class TestDocType(Document):
         pass

@frappe.whitelist()
def test_whitelisted_function(first = None, second = None):
	import requests
		return 'Something'

Hope it will work .

1 Like

I did, and now I can’t seem to be able to frappe.call this function… :-\

If I try to call it using:
frappe.call('test_whitelisted_function'...
in the client script I get: Failed to get method for command test_whitelisted_function with test_whitelisted_function
where as when trying:
frappe.call('test_app.test_app.doctype.test_doctype.test_whitelisted_function'...
in the client script I get:
Failed to get method for command test_app.test_app.doctype.test_doctype.test_whitelisted_function with module 'test_app.test_app.doctype.test_doctype' has no attribute 'test_whitelisted_function'

Thank @saakib10 .
And sorry for the stupidity…
The answer was in front of me all along…
should have been:

from frappe.model.document import Document
import frappe

class TestDocType(Document):
	@frappe.whitelist()
	def test_whitelisted_function(first = None, second = None):
		import requests
		return 'Something'

and the problem was the call, which was frappe.call('test_app.test_app.doctype.test_doctype.test_doctype.test_whitelisted_function',{'first': first,'second': second}) but should have been:
frm.call('test_app.test_app.doctype.test_doctype.test_doctype.test_whitelisted_function',{'first': first,'second': second}) (mind the fmr and not frappe)

1 Like