ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

How to get number of current row of child table

Hi everyone,
I have a usecase that is when i click link field of childtable i would like to display current row’s number for example

if i click INS/00006 i need to get one or zero as result.

Are you referring to the Serial No. which is already a first column of the table? If no, please further elaborate, may be with some annotation.

this is my custom script js ffile

frappe.ui.form.on("Team", "instructor", function(frm,cdt,cdn) {

			
			frappe.call({
					method: "ims.ims.doctype.fe_settings.fe_settings.get_employee_details",
					args: {
						"instructor_id": **current_instructor_id**,
					},
						async: false,
					callback: function(r) {
						if(r.message) {

								locals[cdt][cdn]['instructor_name'] =r.message[0]["instructor_name"];
								locals[cdt][cdn]['phone_number'] =r.message[0]["cell_number"];
								locals[cdt][cdn]['designation'] =r.message[0]["designation"];
								locals[cdt][cdn]['email'] =r.message[0]["company_email"];
								locals[cdt][cdn]['image'] =r.message[0]["image"];
								// refresh_field("team");
								refresh_field("instructor_name");
								refresh_field("phone_number");
								refresh_field("designation");
								refresh_field("email");
								refresh_field("image");
							}
					}
				});	

			
});

i would like to get the current instructor id as INS/00006
Thanks for replying fast

try following script

frappe.ui.form.on('Table Doctype Name', {
	link_fieldname: function(frm,cdt, cdn){
		var row = locals[cdt][cdn]
		frappe.call({
			method: "ims.ims.doctype.fe_settings.fe_settings.get_employee_details",
			args: {"instructor_id": row.idx},
			async: false,
			callback: function(r) {
				//do your operations here
			}
		})
	}
})
1 Like

Hello;

Is there something like this?

var row = previous[cdt][cdn];

It is required to set and get the value of the previous row, and it is much more better if there is a method to access (set/get) any field value in any row in the child table, appreciate if to know the syntax of the code to reach it.

Regards
Bilal

is it possible to set the value of a field of the current child table row ?

Basically i would like to set the default user warehouse for the current row of the sales order item.

if yes how ?

the below script updates all rows as per the each loop

frappe.ui.form.on('Sales Order', {
onload_post_render: function(frm, cdt, cdn)
	{
		frm.fields_dict.items.grid.wrapper.on('blur', 'input[data-fieldname="item_code"][data-doctype="Sales Order Item"]', function(e) {
		
		frappe.call({
			method: "frappe.client.get",
			args: {
				doctype: "User",
				filters: {"name": frappe.user.name} ,//user is current user here
				fieldname :["warehouse"]   
			},
			callback: function(r) {
					$.each(frm.doc.items || [], function(i, v) {
					frappe.model.set_value(v.doctype, v.name,"warehouse",r.message.warehouse)
				})
				frm.refresh_field('items');
		}
		});
				


});
}
});

hi
hope this can help you

cur_frm.open_grid_row()

Nopes. tried on console gives undefined.

OR may be i don’t know how to use it.

Could you please elaborate seeing my script how to place it instead of $.each ?

Try something like this

frappe.ui.form.on('Sales Order', {
	onload: function(frm) {
		frappe.call({
			method: "frappe.client.get",
			args: {
				doctype: "User",
				filters: {"name": frappe.user.name} ,//user is current user here
				fieldname :["warehouse"]   
			},
			callback: function(r) {
				frm.set_value("set_warehouse", r.message.warehouse)
			}
		});
	}
});

If you’re not on v11 then use the $.each(frm.doc.items, …)

Thanks for your solution, it will work great in v11.

But at present i am not on v11.
Let me put more clarity on my requirement, i would like to set default warehouse only when new row / item is selected but user should be able to change it afterwards.

i tried using $.each(frm.doc.items, …) as mentioned above but it updates warehouse for all rows, which will eventually overwrite/update even those rows for which user wanted to change the warehouse.

Try something like this then

frappe.ui.form.on('Sales Order', {
	items_add: function(frm, cdt, cdn) {
		frappe.call({
			method: "frappe.client.get",
			args: {
				doctype: "User",
				filters: {"name": frappe.user.name}, //user is current user here
				fieldname : "warehouse"   
			},
			callback: function(r) {
				frappe.model.set_value(cdt, cdn, "warehouse", r.message.warehouse)
			}
		});
	}
});

Thanks for your kind help.

Tried.

But unable to set the value, it seems the erpnext function
erpnext.stock.get_item_details.get_item_details
is being called just after my call, which overrides the value.

It seems , the only option left is to override the function
erpnext.stock.get_item_details.get_item_details
through custom app.

‘onload_post_render’: function(frm, cdt, cdn) {
var list = frm.doc.entry_dashboard_list;
frm.fields_dict.entry_dashboard_list.grid.wrapper.on(‘focus’, ‘input[data-fieldname=“new_entry”][data-doctype=“Entry Dashboard List”]’, function(e) {
var current_doc = $(’.data-row.editable-row’).parent().attr(“data-name”);
var d = locals[“Entry Dashboard List”][current_doc];

Here,
entry_dashboard_list - Child table name,
new_entry - child table field name
click on a any row in the new_entry(column name in a child table)field you can access that full row by d.field_name

Hi @vivin_joseph,

I too was looking for something similar, like
When i create a new row or i click on any of the fields of child table, i get to know the exact position of that row.
I was not able to get a direct solution for that, but have find a work around but with correct output

In your case, within Team(Child Table) you can add an event on the Instructor(field)

instructor: function(frm, cdt, cdn){
    // cdn is the unique number of the current row the event has occurred, and using cdn you can map the index of your row inside your child table filtering the array and comparing the cdn with name of child table row 
}

This is a working example from my code,
Please let me know if you were able to solve your issue with this one