ERPNext Foundation ERPNext Cloud User Manual Blog Discuss Frappé* Donate

Child table fetching data problem


#1

I have created a doctype named TimeLog .Its has a child table named log table . log table has 3 fields .in_time,out_time , time difference. Now the problem is i want my user can enter in_time and out_time and after that when he will press the update button or save button time difference will be calculated and will be showed in the ui. Now , for that i have to take entered values by user.But i am unable to find any solutions so far…i have been stuck here for almost 4 days.Please help me someone as i am newbie in erpnext development. Here is a picture so that you can understand my problem properly:


#2

Hi @Arif_123,

have you looked into the custom scripts? A good starting point is the Developer Cheatsheet

Add a before_save or validate trigger, loop through the items and update the field (maybe make the difference field read-only).

Hope this helps.


#3

@Arif_123

you can calculate the time difference on before_save or after_save:

frappe.ui.form.on('Booking', "after_save", function(frm) {

    if (cur_frm.doc.log_table != undefined) {
        for (var i = 0; i < cur_frm.doc.log_table.length; i++) {
            cur_frm.doc.log_table[i].time_difference = cur_frm.doc.log_table[i].out_time - cur_frm.doc.log_table[i].in_time;
        }
        cur_frm.refresh_fields('log_table');
    }
}

Hope this Solve yours query.


#4

Bro, i tried your piece.But unfortunately it didnot work for me in my case. Don’t know why.Can you suggest me any alternate way?


#5

what error you got?
And can you share yours piece of code too.


#6

Brother it didnot show me error that’s the main problem. I opened the console and checked whether there is any error or not.But i found nothing.Here is the piece of my code.my parent doctype name is “TimeLog”.


#7

@Arif_123
For Time Difference Please Refer below link


that will help you in calculating the time difference.

Error is that in_time and out_time these two are string values that why the above code was not working.


#8

Bro, i have done everything as you instructed:

Still no improvement. My TIME DIFFERENCE field is READ ONLY TYPE so i parsed hour,minute,second to String like this:

cur_frm.set_value(“time_difference”,hour.toString()+":"+minute.toString()+":"+second.toString());

and ran bench build command. Still failing every time :’(

now feeling useless as a developer :’( :’(


#9

Just replace below code with your code

frappe.ui.form.on('TimeLog', "before_save", function(frm) {
    if (cur_frm.doc.log_table != undefined) {
        var hour=0;
        var minute=0;
        var second=0;
        var outTime = 0;
        var inTime = 0;
        for (var i = 0; i < cur_frm.doc.log_table.length; i++) {
            outTime = cur_frm.doc.log_table[i].out_time;
            inTime = cur_frm.doc.log_table[i].in_time;
            var splitOutTime= outTime.split(':');
            var splitInTime= inTime.split(':');
            hour = Math.abs(parseInt(splitOutTime[0])-parseInt(splitInTime[0]));
            minute = Math.abs(parseInt(splitOutTime[1])-parseInt(splitInTime[1]));
            hour = hour + minute/60;
            minute = minute%60;
            second = Math.abs(parseInt(splitOutTime[2])-parseInt(splitInTime[2]));
            minute = minute + second/60;
            second = second%60;
            cur_frm.doc.log_table[i].difference_time = parseInt(hour)+':'+parseInt(minute)+':'+parseInt(second)
        }
        cur_frm.refresh_fields('log_table');
    }
});

Don’t worry! You’ll find a way out. Happy Practicing!