Hi,
Is there any way to add records in child table at a particular row number using custom script?
If yes, I really appreciate if anyone can let me know how to do it.
Regards
Ruchin Sharma
Hi,
Is there any way to add records in child table at a particular row number using custom script?
If yes, I really appreciate if anyone can let me know how to do it.
Regards
Ruchin Sharma
I think you will have to play with the idx field, the one that identifies the position in the table.
I don´t have access to my dev environment here but will try to make an example later
@Pau_Rosello_Van_Scho
Thanks for your quick response, actually I know for this I have to play with idx field but don’t know how. I will wait for your example on the same.
Regards
Ruchin Sharma
Try with this snippet:
function add_child_position(doc,table_name,doctype,position){
// item bigger than length
if(position>doc[table_name].length){
var row = frappe.model.add_child(doc, doctype, table_name);
return row;
}
// item less than first
else if(position < doc[table_name][0].idx){
var row = frappe.model.add_child(doc, doctype, table_name);
row.idx = position;
return row;
}
// item in the middle
else{
for(var curr_pos= position; curr_pos<doc[table_name].length; curr_pos++){
doc[table_name][curr_pos].idx+=1;
}
var row = frappe.model.add_child(doc, doctype, table_name);
row.idx = position;
return row;
}
}
In Sales order you can call it like this:
var item = add_child_position(cur_frm.doc, "items", "Item",2)
//modify item
cur_frm.refresh_field("items")
@Pau_Rosello_Van_Scho
Thanks for your response on the same, I will definitely try this code. I just have one query regarding this code, since the function is taking the parameters:
function add_child_position(doc,table_name,doctype,position){
and we are passing parameters as shown below:
add_child_position(cur_frm.doc, "items", "Item",2)
As per the parameters passed:
doc
will be cur_frm.doc
table_name
will be items
doctype
will be Item
position will be 2
My only question is why doctype
is Item
here in this case?
Regards
Ruchin Sharma
This script in the end is calling the standard frappe.model.add_child(doc, doctype, table_name); in which the doctype is refering to the type of the child table