Using frappe.client.get_value from web form

Hi,
I’m trying to get value using frappe.client.get_value from a web form.
I put the js code on the Client Script tab of the web form.
The webform is for guest so users are not logged in.
The value I try to get is a field in a custom app.

When the code runs in the webform, I got popup saying Not Permitted.
How do I get around this?
Thank you.

The DocType you’re trying to access probably isn’t readable by Guest.

You can allow Guest to read it using Role Permissions Manager or write another endpoint that is readable by Guest.

Either create a Server Script (Script Type: API) and check Allow Guest.

Or write a whitelisted method with

frappe.whitelist(allow_guest=True)
def anything_goes():
    pass

Thanks @adityahase,

I tried both. What is not throwing exception anymore is to make own whitelisted API. I copied the get_single_value from frappe and giving the allow_guest=True.
But it still doesn’t work because the browser console now giving Error 500.

What I want to achieve is when a field in webform is filled with name, the code retrieve the address from a setting (single doctype) and return it to the address field in the webform.

This is my code:
js:

frappe.web_form.on('form_nama', () => {
    frappe.call({
        method: "appname.api.get_alamat",
        args: {
        "doctype": "Akun Settings",
        "fieldname": "akun_alamat"
        }, callback: function(r) {
            frappe.web_form.set_value('form_alamat', r);
        }
    });
});

py:

@frappe.whitelist(allow_guest=True)
def get_alamat(doctype, field):
	if not frappe.has_permission(doctype):
		frappe.throw(_("No permission for {0}").format(doctype), frappe.PermissionError)
	value = frappe.db.get_single_value(doctype, field)
	return value
1 Like

It’s tough (except in some cases) to reply to “It doesn’t work.”

Can you provide more information?

e.g. Is there a message/stack trace in the browser console? Any response included with the 500 status code? Any message/stack trace from bench start, or logs/web.log and logs/web.error.log if you’re running in production.

No other message with the 500 error code. Only 1 line with that msg.
From the web.error.log a long list of:

[2020-12-21 16:37:46 +0700] [79942] [INFO] Worker exiting (pid: 79942)
[2020-12-21 16:37:46 +0700] [80062] [INFO] Booting worker with pid: 80062

And from web.log:

b'Traceback (most recent call last):\n  
File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 50, in render\n    
data = render_page_by_language(path)\n  
File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 177, in render_page_by_language\n    
return render_page(path)\n  
File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 193, in render_page\n    
return build(path)\n  
File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 200, in build\n    
return build_page(path)\n  
File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 218, in build_page\n    
html = frappe.render_template(context.source, context)\n  
File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/jinja.py", line 80, in render_template\n    
return get_jenv().from_string(template).render(context)\n  
File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render\n    
return original_render(self, *args, **kwargs)\n  
File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render\n    
return self.environment.handle_exception(exc_info, True)\n  
File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception\n    
reraise(exc_type, exc_value, tb)\n  
File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise\n    
raise value.with_traceback(tb)\n  
File "<template>", line 5, in top-level template code\n  
File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/sandbox.py", line 440, in call\n    
return __context.call(__obj, *args, **kwargs)\n  
File "/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py", line 549, in get_single_value\n    
if fieldname in self.value_cache[doctype]:\n
TypeError: unhashable type: \'list\'\n'

The traceback seems to point to a Jinja template, not a whitelisted method.

Also, are you sure you’ve pasted corrected code above? the client side code uses doctype and fieldname but the server side code uses field.

Yes I missmatched that field and fieldname
Must be the late night effect :slight_smile:
Thanks a lot.

@rahy
Can you please share your correct snippet here

I think I don’t use that codes anymore.
But you can see from my code above and play with it.