Hi, i am currently creating a survey using the web from and a doctype .
I need to pre set data in the child table row for users to leave rating.
Hi, i am currently creating a survey using the web from and a doctype .
I need to pre set data in the child table row for users to leave rating.
@Vasanth_Iyyanar_Rang Welcome to ERPNext community.
You can achieve what you want by creating a Client Script, set the doctype as Web Form and the code will be like the following.
Assuming that the child table fieldname is “items”
frappe.ui.form.on("Web Form", {
refresh: function(frm) {
// check if child table fieldname exists in the doctype or not
if (!frm.doc.items) return;
frm.add_child('items', {
item_code: 'Tennis Racket',
qty: 2
});
// finally refresh the child table
frm.refresh_field('items');
}
});
Dear @kid1194 , Thank you so much for your help ,i did create a client script and chose web form as doctype.
for some reason its not working .
frappe.web_form.set_df_property([fieldname], [property], [value]);
will this work for child table ?
@Vasanth_Iyyanar_Rang Sorry, the code I posted is not ment to work with Web Forms.
What you want to do can’t be done directly, so the following code is based on frappe source code and it is supposed to be a workaround. i hope it works for you.
frappe.web_form.after_load = function() {
let me = frappe.web_form;
// check if the web form is what you are looking for
if (me.doc.doctype !== "Survey") return;
// check if child table fieldname exists in the doctype or not
if (!me.has_field('items')) return;
let field = me.get_field('items');
// check if field is table
if (!field.grid) return;
let row = field.grid.add_new_row(null, null, true, null, true),
grid_rows = field.grid.grid_rows,
row_name = grid_rows[grid_rows.length - 1].doc.name;
frappe.model.set_value(field.grid.doctype, row_name, "fieldname", "value");
}
};
frappe.ready(function() {
frappe.web_form.after_load = function() {
let me = frappe.web_form;
console.log(me);
// check if the web form is what you are looking for
if (me.doc.doc_type !== "Unit Food Survey") return;
// check if child table fieldname exists in the doctype or not
if (!me.has_field('use_food_type_restaurant')) return;
let field = me.get_field('use_food_type_restaurant');
// check if field is table
if (!field.grid) return;
let row = field.grid.add_new_row(null, null, true, null, true),
grid_rows = field.grid.grid_rows,
row_name = grid_rows[grid_rows.length - 1].doc.name;
frappe.model.set_value(field.grid.doctype, row_name, "produces_name", "Milk");
}
})
Dear One , This are the corrected field names.but still its not functioning as expected.
DId you get solution for this…??
Yes, i managed get it to work
This works for me, try it and mention me if any issue.
frappe.web_form.on('referenced_request_for_quotation', (field, value) => {
if (value) {
frappe.call({
method: 'frappe.client.get',
args: {
doctype: 'Request for Quotation',
name: value
},
callback: function(response) {
if (response.message) {
let items = response.message.items || [];
console.log("items", items);
let field = frappe.web_form.get_field('items');
items.forEach((item, index) => {
// Add a new row
field.grid.add_new_row();
// Get the last row in the grid after adding
let grid_rows = field.grid.grid_rows;
let last_row = grid_rows[grid_rows.length - 1];
// Set values in the newly added row
last_row.doc.item_code = item.item_code;
last_row.doc.qty = item.qty;
last_row.doc.uom = item.uom;
last_row.doc.rate = item.rate;
last_row.doc.amount = item.amount;
last_row.doc.warehouse = item.warehouse;
// Refresh the last row to reflect the changes
last_row.refresh();
});
}
}
});
}
});