Call Server Script Funtion from Client Script

All information in this forum leads to creating a Custom Apps. Is it correct that API calls is not possible.

I have this function ‘add_qty()’ that run successfully in System Console. How can I call this from Client Script? Just hoping this is a method.

def add_qty():
    
    # Specify Work Order and Additional Qty
    work_order_id = "JO-2511011" # Change this value as needed
    added_qty = 2000  # Change this value as needed

    new_status = "In Process"

    # Fetch the current qty
    current_qty = frappe.db.get_value("Work Order", work_order_id, "qty") or 0

    # Calculate new quantity
    new_qty = current_qty + added_qty

    # Update Work Order fields
    frappe.db.set_value("Work Order", work_order_id, "status", new_status)
    frappe.db.set_value("Work Order", work_order_id, "qty", new_qty)
    frappe.db.set_value("Work Order", work_order_id, "added_qty", added_qty)

    # Commit changes
    frappe.db.commit()

    frappe.msgprint(f"Work Order {work_order_id} updated!\nStatus: '{new_status}'\nNew Qty: {new_qty}\nAdded Qty: {added_qty}")

# Call the function
add_qty()

You can use frappe.call to call api methods

Frappe.call can call API created in Custom Apps but not the one created in Server Script.

Custom app is not mandatory but its good to have because it will keep original source code intact. Second, you can call any server side function from client script. Store it in any app, use dotted path for that method, like app.module.file_name.function_name. Make sure to whitelist the server function by using frappe.whitelist() just before the def add_qty() line

1 Like

This is incorrect. It is possible to call Sever Scripts (type “AP”) via frappe.call.

2 Likes

There several types of Server Script. And you can call some of them via client script.

Hi @jcagbay , you asked how to call your function from Client Script.

You can add this code in your client script to call your function:

frappe.ui.form.on('[DocType name]', '[button_name]', function(frm) { // assuming you have a button in your doc_type
        
        frappe.call({
            method: "[api_method_name]",
            args: { args: frm.doc }, // argument being passed to the server script
            async: true, // or false case you don't want to wait for the processing
            callback: function(response) {
                frappe.msgprint(response['message']); // it will pop-up a message if you function returns something
            }
        });
        
});

To make this work you must create a server script and add the [api_method_name] in the API Method field

The script type must be “API” as well.

Example:

def add_qty(args): # you don't need to use the argument but you can and it is the frm.doc being passed
   
    frappe.response['message'] = 'server script called'

add_qty(args) // don't forget to call your function here.  IF you miss this line, nothing will happens!!!

PS: Deepseek and others IA leads to simliar solutions and works well for this kind of question.

Let us know if you get it working. good luck

2 Likes

@totalys It is working, thank you for your help.

1 Like