Server script to create new doc via an AJAX call

Hi There,

I am writing a server script that creates a new lead doc using an AJAX call. I am using a web template to make the AJAX call. Unfortunately, my script is not working and is not producing any error messages. I am able to see a success request but it doesn’t create the new lead doc. Any help would be greatly appreciated.

Here is my server script code:

def create_lead(first_name, last_name, email, message=None):
        lead = frappe.get_doc({
            "doctype": "Lead",
            "lead_name": f"{first_name} {last_name}",
            "email_id": email,
            "status": "Lead"
        })
        lead.insert(ignore_permissions=True)
        frappe.db.commit()

        if message:
            communication = frappe.get_doc({
                "doctype": "Communication",
                "communication_type": "Communication",
                "subject": "Lead Inquiry Message",
                "content": message,
                "communication_medium": "Other",
                "reference_doctype": "Lead",
                "reference_name": lead.name
            })
            communication.insert(ignore_permissions=True)
            frappe.db.commit()

Here’s my AJAX call script :

<script>
    function submitForm() {
        // Gather form data
        const formData = {
            first_name: document.getElementById('first_name').value,
            last_name: document.getElementById('last_name').value,
            email: document.getElementById('email').value,
            message: document.getElementById('message').value
        };

        // Send AJAX request to server script
        fetch('/api/method/create_lead', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
               'X-Frappe-CSRF-Token: frappe.csrf_token
            },
            body: JSON.stringify(formData)
        })
    }
</script>

It will be /api/method/path.to.module.create_lead

If possible use frappe.call instead of fetch(), it manages a lot of things internally. Frappe Ajax Call

1 Like

Hi @revant_one,

I went ahead and revised my JS code to use frappe.call as suggested. However, I’m still encountering the same issue. It appears that the request is successful but, it is not creating a lead for some reason.

Frappe_AJAX_troubleshooting1

Here’s is my revised code:

<script>
    function submitForm() {
        // Gather form data
        const formData = {
            first_name: document.getElementById('first_name').value,
            last_name: document.getElementById('last_name').value,
            email: document.getElementById('email').value,
            message: document.getElementById('message').value
        };

        // Send AJAX request to the server script
        frappe.call({
            method: 'create_lead',
            type: "POST",
            success: function(r) {
                frappe.msgprint("Lead creation successful!");
            },
            error: function(r) {
                frappe.msgprint("Lead creation failed!");
            },
            args: {
            doctype: "Lead",
            email_id: formData.email,
            status: "Lead",
            message: formData.message
        },
        callback: function(r) {
            frappe.msgprint("Successfull Callback Message!");
        }
        });
        }
</script>

Not sure what I’m missing. In reference to the “path.to.module”, how do I determine the path? It seems like I’m able to make the call just fine with simply entering the name of the API Method. Unfortunately, I wasn’t able to find any documentation.

Thank you and I appreciate all your help!

Means Python Path of your API.

Example:

API Defination

Calling an API

  • Basically relative path from your app.

  • You can write it manually or If you are using Vs Code you can right click on api line and copy Python Path (App should be open as Working Directory).

Extension:
Copy Python Path Extension

2 Likes

Thanks for all your help. I managed to figure it out. First, I need to clarify that this is a server script doc type. I am sending an AJAX request to the server script from the front end. The goal is to use the data that came with the request to create a new Lead. To do this I had to define the values accordingly using > frappe.form_dict

Here’s my revised server script:

first_name = frappe.form_dict.first_name
last_name = frappe.form_dict.last_name
email_id = frappe.form_dict.email_id
message = frappe.form_dict.message

# Create Lead document
lead = frappe.get_doc({
    "doctype": "Lead",
    "lead_name": f"{first_name} {last_name}",
    "email_id": email_id,
    "status": "Lead"
})
lead.insert(ignore_permissions=True)

# Create Communication if message exists
if message:
    communication = frappe.get_doc({
        "doctype": "Communication",
        "communication_type": "Communication",
        "subject": "Lead Inquiry Message",
        "content": message,
        "communication_medium": "Other",
        "reference_doctype": "Lead",
        "reference_name": lead.name
    })
    communication.insert(ignore_permissions=True)

frappe.db.commit()