I have a customisation where in a doctype i added a field table which is a child table in that i have a column crop_name where i want to fetch the options from custom API where i have added the script in backend when called from client side to return the values.
For some reason the client script didn’t work. I want help related to what should the data type be for the field crop_name and what’s the best approach to achieve this
Attaching screenshot for reference
Update Crop Name fieldtype to Autocomplete
.
In your parent doctype’s js file:
frappe.ui.form.on("PARENT_DOCTYPE", {
setup: function (frm) {
frm.set_query("crop_name", "FIELD_NAME_OF_TABLE", (_, cdt, cdn) => {
return {
query: "API_PATH",
params: {a:"test-arg-a",b:"test-arg-b"},
};
});
},
});
and at server side:
@frappe.whitelist()
def get_crop_names(a=None, b=None) -> list:
# your logic to fetch crop names
# example crop names
return ["Rice", "Wheat", "Sugarcane", "Cotton", "Groundnut", "Soybean", "Mustard"]
I hope this may be helpful
Tried This approach client script was able to call backend and get response but in autocomplete field options were not showing
frappe.ui.form.on('Village Profile', {
onload: function(frm) {
// Fetch crop names from the server
frappe.call({
method: 'customapp.customapp.doctype.village_profile.village_profile.get_crop_names',
callback: function(r) {
if (r.message) {
// Store the crop names in a cache
frm._crop_cache = r.message.map(c => c.crop_name);
// Set the options for the crop_name field in each row of the child table
frm.fields_dict.cropping_pattern.grid.grid_rows.forEach(function(row) {
const df = frappe.meta.get_docfield('Cropping Pattern', 'crop_name', frm.doc.name);
df.options = frm._crop_cache;
frm.refresh_field('cropping_pattern');
});
}
}
});
}
});
In above post I have mentioned client side code use that.
(params
is not mandatory)