How to create form to send variables to an api call?

I have a server script set to api and it is working good.

Currently, I am changing the variables in the server script then going to another tab in the browser and calling the api.

I am hoping to create a form where I fill in the needed values for the variables then press a button.

This button will call the server script api with the variables and substitute into the server script and run the server script.

Is this possible?

If yes, how to do it.

Any links to some documentation would be great.

  • create the new doctype
  • check this checkbox to create a form

  • can call the api in after save in js file

u can also find a way to call the third party api

Thank you very much for sharing the information. Now I have a starting point to work on.

Thank you again for sharing. I do appreciate it very much.

You’re very welcome. I’m glad the information was helpful and gave you a good starting point to work from.

Please note that I am not a programmer.

I am just sharing my amateur code here for others to learn from and for experts to make it better.

The purpose of creating a doctype in erpnext so that my collegue can simply fill a form and press a button and the data plus the customer data will go into another application using api.

This way, there is no customer data in some exported csv file on the laptop. This is to comply with personal data protection.

First we make the api using server script in erpnext. Here is what I used. Set the server script to api.

campaign_id = frappe.form_dict.get('campaign_id', 0)

param1 = frappe.form_dict.get('param1', 0)
param2 = frappe.form_dict.get('param2', 0)
param3 = frappe.form_dict.get('param3', 0)
param4 = frappe.form_dict.get('param4', 0)
param5 = frappe.form_dict.get('param5', 0)
param6 = frappe.form_dict.get('param6', 0)
param7 = frappe.form_dict.get('param7', 0)
param8 = frappe.form_dict.get('param8', 0)
param9 = frappe.form_dict.get('param9', 0)
param10 = frappe.form_dict.get('param10', 0)

sql = f"""SELECT
    cu.customer_mobile_number AS "phone_number",
    cu.name AS "name", 
    '{param1}' AS "param1",
    '{param2}' AS "param2",
    '{param3}' AS "param3",
    '{param4}' AS "param4",
    '{param5}' AS "param5",
    '{param6}' AS "param6",
    '{param7}' AS "param7",
    '{param8}' AS "param8",
    '{param9}' AS "param9",
    '{param10}' AS "param10"
FROM
    `tabSales Order` so
LEFT JOIN
    `tabSales Order Item` soi
    ON soi.parent = so.name
LEFT JOIN
    `tabCustomer` cu
    ON cu.name = so.customer_name
WHERE
    soi.booking_for = "Something"
    AND DATE(so.creation) > "2025-12-31"
    #AND DATE(so.creation) = CURRENT_DATE()
GROUP BY
    cu.customer_mobile_number
ORDER BY
    so.creation
"""


doc = frappe.db.sql(sql, as_dict=True)

headers = {
    "Content-Type": "application/json",
    "X-API-Key": "api key on the other application",
}

payload = {"recipients":[]}

for event in doc:
    payload["recipients"].append({"phone_number": event.phone_number,
        "recipient_name": event.name,
        "template_params":{
            "1": event.param1,
            "2": event.param2,
            "3": event.param3,
            "4": event.param4,
            "5": event.param5,
            "6": event.param6,
            "7": event.param7,
            "8": event.param8,
            "9": event.param9,
            "10": event.param10
            }
        })
        
frappe.make_post_request(
     "https://other_application.example.com/api/campaigns/" + campaign_id + "/recipients/import", 
     json = payload,
     headers = headers
  )

#frappe.response['message'] = payload  
    

Next create a new doctype with the setting “Single” and the module “Desk”. Then create a form for the params needed.

Next create the button using client script.

frappe.ui.form.on('Name of the new doctype', {
    refresh: function(frm) {
            frm.add_custom_button(__('Send Data to Other app'), function() {
                //frappe.msgprint('Sending data to Other app.');
                frappe.call({
                    method: 'the method name in the server script',
                    args: {
                        'campaign_id': frm.doc.fieldname1,
                        'param1': frm.doc.fieldname2,
                        'param2': frm.doc.fieldname3,
                        'param3': frm.doc.fieldname4,
                        'param4': frm.doc.fieldname5,
                        'param5': frm.doc.fieldname6,
                        'param6': frm.doc.fieldname7,
                        'param7': frm.doc.fieldname8,
                        'param8': frm.doc.fieldname9,
                        'param9': frm.doc.fieldname10,
                        'param10': frm.doc.fieldname11
                    },
                    callback: function(r) {
                        if(!r.exc){
                        //if(r.message.status !== "success") {
                            //frappe.msgprint(__("Data Sent Successfully!"));
                            //console.log(r.message.recipients[0].phone_number);
                            console.log(frm.doc.fieldname1);
                            console.log(frm.doc.fieldname2);

                            frappe.msgprint(__("Sent to Other app Successfully!"));
                            //frm.refresh();
                        //}
                        } else {
                            frappe.msgprint(__('Error: {0}').format(r.message.message || 'Unknown error'));
                        }
                    }
                });
            });
    }
});

Hope this helps someone.

1 Like

Hi @pvanthony

You should mark your answer as the sollution for reference purposes

1 Like

Thank you for reminding.

It is done now.