Fetch values from table for select list

@Bhargav_N Sorry, my mistake…

  • Set the server script type as API
  • Set the method name as get_item_process_list
  • Use the following codes…

Server Script

if not doc.component or not isinstance(doc.component, str):
    frappe.response["message"] = []

else:

    try:
        data = frappe.get_all(
            "Item Process Table",
            fields=["pn"],
            filters={
                "parent": doc.component,
                "parenttype": "Item Process List",
                "parentfield": "ipt",
            },
            limit_page_length=100,
        )
        
        frappe.response["message"] = data if isinstance(data, list) else []
    except Exception:
        frappe.response["message"] = []

JavaScript

frappe.ui.form.on('Tool Setup and Program Control Plan', {
    onload: function(frm) {
        // Cache for setting_name options
        frm._setting_names = {};
    },
    refresh: function(frm) {
        frm.trigger('load_process_names');
    },
    component: function(frm) {
        frm.trigger('load_process_names');
    },
    load_process_names: function(frm) {
        var select = frm.get_field('setting_name'),
        val = frm.doc.setting_name || null,
        // The select list options with the default first option
        list = [{label: __('Select Process...'), value: ''}];
        // If the component field is empty, then empty the select list
        if (!frm.doc.component) {
            // Adding the old value of setting_name (if there is any) to the select list
            if (val) list.push({label: val, value: val});
            // Adding the list of options to the select field
            select.df.options = list;
            // Selecting the old value of setting_name if there is any
            select.set_options(val);
            return;
        }
        // A function to add the list of options to the select field 
        var setOptions = function(optList) {
            // Adding the list of process names to the select field
            select.df.options = list;
            // Selecting the old value of setting_name if there is any
            select.set_options(val);
        };
        // If cache exist, the get options from cache
        if (frm._setting_names[frm.doc.component]) {
            setOptions(frm._setting_names[frm.doc.component]);
            return;
        }
        // Getting the process name values
        frappe.call('get_item_process_list')
        .then(function(data) {
            if (!data || !data.message || !$.isArray(data.message)) {
                frappe.throw(__('Unable to get the process list'));
                return;
            }
            data = data.message;
            // Sorting the process name from A-Z
            data = frappe.utils.sort(data, 'pn');
            // Making sure that the list of process name doesn't have duplicate values
            let check = [];
            data.forEach(function(v) {
                if (check.indexOf(v.pn) < 0) {
                    check.push(v.pn);
                    list.push({label: v.pn, value: v.pn});
                }
            });
            // Storing list in Cache
            frm._setting_names[frm.doc.component] = list;
            setOptions(list);
        });
    }
});

Server Script Name: get_item_process_list
Script Type: API
API Method: get_item_process_list

is it right??

@Bhargav_N Yes, that’s right…

Did it work?

Sorry for the late reply.
I’m getting this error.

App Versions

{
	"erpnext": "14.10.1",
	"frappe": "14.19.1",
	"hrms": "1.0.0",
	"india_compliance": "14.0.5",
	"payments": "0.0.1"
}

Route

Form/Tool Setup and Program Control Plan/new-tool-setup-and-program-control-plan-2

Trackeback

Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 69, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 54, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 45, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 69, in execute_cmd
    return run_server_script(server_script)
  File "apps/frappe/frappe/handler.py", line 87, in run_server_script
    response = frappe.get_doc("Server Script", server_script).execute_method()
  File "apps/frappe/frappe/core/doctype/server_script/server_script.py", line 89, in execute_method
    _globals, _locals = safe_exec(self.script)
  File "apps/frappe/frappe/utils/safe_exec.py", line 72, in safe_exec
    exec(compile_restricted(script), exec_globals, _locals)  # pylint: disable=exec-used
  File "<unknown>", line 1, in <module>
NameError: name 'doc' is not defined

Request Data

{
	"type": "POST",
	"args": {},
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/get_item_process_list"
}

Response Data

{
	"exception": "NameError: name 'doc' is not defined"
}

@Bhargav_N Sorry, my mistake…

I thought that the doc variable will be available for this type of server script…

Because of that, we have to send the component value with the request…

I have modified both codes a bit…

Give it a try and see if it’s working or not…

Server Script

component = frappe.form_dict.component
if not component or not isinstance(component, str):
    frappe.response["message"] = []

else:

    try:
        data = frappe.get_all(
            "Item Process Table",
            fields=["pn"],
            filters={
                "parent": component,
                "parenttype": "Item Process List",
                "parentfield": "ipt",
            },
            limit_page_length=100,
        )
        
        frappe.response["message"] = data if isinstance(data, list) else []
    except Exception:
        frappe.response["message"] = []

JavaScript

frappe.ui.form.on('Tool Setup and Program Control Plan', {
    onload: function(frm) {
        // Cache for setting_name options
        frm._setting_names = {};
    },
    refresh: function(frm) {
        frm.trigger('load_process_names');
    },
    component: function(frm) {
        frm.trigger('load_process_names');
    },
    load_process_names: function(frm) {
        var select = frm.get_field('setting_name'),
        val = frm.doc.setting_name || null,
        // The select list options with the default first option
        list = [{label: __('Select Process...'), value: ''}];
        // If the component field is empty, then empty the select list
        if (!frm.doc.component) {
            // Adding the old value of setting_name (if there is any) to the select list
            if (val) list.push({label: val, value: val});
            // Adding the list of options to the select field
            select.df.options = list;
            // Selecting the old value of setting_name if there is any
            select.set_options(val);
            return;
        }
        // A function to add the list of options to the select field 
        var setOptions = function(optList) {
            // Adding the list of process names to the select field
            select.df.options = list;
            // Selecting the old value of setting_name if there is any
            select.set_options(val);
        };
        // If cache exist, the get options from cache
        if (frm._setting_names[frm.doc.component]) {
            setOptions(frm._setting_names[frm.doc.component]);
            return;
        }
        // Getting the process name values
        frappe.call('get_item_process_list', {component: frm.doc.component})
        .then(function(data) {
            if (!data || !data.message || !$.isArray(data.message)) {
                frappe.throw(__('Unable to get the process list'));
                return;
            }
            data = data.message;
            // Sorting the process name from A-Z
            data = frappe.utils.sort(data, 'pn');
            // Making sure that the list of process name doesn't have duplicate values
            let check = [];
            data.forEach(function(v) {
                if (check.indexOf(v.pn) < 0) {
                    check.push(v.pn);
                    list.push({label: v.pn, value: v.pn});
                }
            });
            // Storing list in Cache
            frm._setting_names[frm.doc.component] = list;
            setOptions(list);
        });
    }
});