ERPNext Foundation ERPNext Cloud User Manual Blog Discuss Frappé* Donate

REST Api from custom HTML form


#1

I created a custom html form with a custom method inside .py file and a custom js file for the html page
and i used REST api to create a new document in my custom doctype which is inside a custom module
i get some errors

my custom module name: Customer Forms
My custom doctype: Contact Us Form
the full_name and email_address fields are only the required fields all of the others are optional and i use series as the Auto Naming “New Message - #####”

here are my codes:

testform.html

<form>
    <div class="form-group">
        <label>Email address</label>
        <input type="email" class="form-control" name="email">
    </div>
    <div class="form-group">
        <label>Full Name</label>
        <input name="fullname" class="form-control" type="text">
    </div>
    <button class="btn btn-primary btn-send">Submit</button>
</form>

testform.js

frappe.ready(function() {

        $('.btn-send').off("click").on("click", function() {
                var email = $('[name="email"]').val();
                var name = $('[name="fullname"]').val();
                              
                frappe.call({
                    method: "erpnext.customer_forms.doctype.contact_us_form.testform.send_contactusform",
                    args: {"full_name": name, "email_address": email},
                    callback: function(r) {
                        if(r.message==="okay") {
                                                            frappe.msgprint("{{ _("Thank you for your message") }}");
                        } else {
                                                            frappe.msgprint("{{ _("There were errors") }}");
                                                            console.log(r.exc);
                        }
                        $(':input').val('');
                    }
                });
        });
             
});

testform.py

from __future__ import unicode_literals

import frappe
from frappe.utils import now
from frappe import _


@frappe.whitelist(allow_guest=True)
def send_contactusform(full_name, email_address):
    
    newmsg= frappe.get_doc({
                        "doctype": "Contact Us Form",
                        "full_name": full_name,
                        "email_address": email_address
                        })
        
    newmsg.insert(ignore_permissions=True)
    return "okay"

the errors i get are:

jquery.min.js:4 POST https://azizplastic.com/ 500 (INTERNAL SERVER ERROR)
send @ jquery.min.js:4
ajax @ jquery.min.js:4
call @ website.js:64
(anonymous) @ testform:254
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3

and

website.js:162 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 1019, in call
    return fn(*args, **newargs)
TypeError: send_contactusform() takes exactly 1 argument (0 given)

and i put these as the inputs of the fields (test, test@email.com)

i donno what is the problem exaclty but if anyone could help me it would be great

and here is my url of the custom form

https://azizplastic.com/testform


#2

try with default values for params in the py

def send_contactusform(full_name="", email_address=""):

if the error still persists then probably your function path is wrong.