Data saving problem using frappe.ui function

Hello, i am trying to save the value in the custom child table but facing problem while using the frappe.ui.form .on function…Here are the details about my problem:

This is my child table. I am calculating two time fields and setting the result in the third table.But when i save it with update button it does not show the result at once. Like this:

Then i reload the whole page and the whole script run and after that i get the desired result. Its after reload the page.

But i want it to behave like the normal way. i want to see the result after submitting the result… How can i do that. I have tried to do them by using their documentation and all other suggestions. Have not found any solution.

Here is the code.

console log was used to check the answers btw.

Trigger the function on in_time and out_time fields. it will calculate on change of in and out time.

1 Like

Brother…can u help me with the convention? just the structure.

Share your complete script here.

frappe.ui.form.on(‘TimeLog’, {
refresh: function(frm) {

    }

});
frappe.ui.form.on(‘TimeLog’, ‘validate’, function(frm) {
if (frm.doc.log_date < get_today() || frm.doc.log_date > get_today()) {
msgprint(‘Please Select Current date’);
validated = false;
}

});
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].time_difference = parseInt(hour)+’:’+parseInt(minute)+’:’+parseInt(second)

 }

}

here is the script

@Arif_123 Your script should be inside of any trigger.
somewhat like this.

frappe.ui.form.on('TimeLog', {
    refresh: 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].time_difference = parseInt(hour)+’:’+parseInt(minute)+’:’+parseInt(second)

            }
        }
    }
});
1 Like

@ROHAN_JAIN1 is correct, script must be inside of trigger, and in your case triggers should In Time and Out time fields name, instead of refresh. so it will calculate difference when In Time or Out time value is changed.

2 Likes

Thank you guys for your efforts. It worked

1 Like

Thanks man

1 Like

Brother, today i am doing thje same thing in the parent doctype…There is field name is Late. The field is a read only field and it will show yes or no if an employee comes late at the office. I am tring to triggering the field but nothing is changing… I am providing some screen shot and please point out where i am doing the blunder.

Here is the structure:

Here is my script:

prob1

Here is my function:

@ROHAN_JAIN1 please help me on this brother

@shahid brother u too

Hello @Arif_123,

You are setting the value of is_late field to be yes or no.
The issue here is you need to change the field name to be triggered upon. You are calculating the value of is_late based on the office_in_time. So you need to keep office_in_time in the trigger field.

Somewhat like this

    frappe.ui.form.on('Time Log', {
    office_in_time: function(frm,cdt,cdb){
    	    console.log("In Time Changes!")
    	    frm.set_value('is_late',get_late_status(frm))
    	}
    })
4 Likes

brother, i’ve tried…its not even printing the console.What could be the reason?

@Arif_123
Is your document save/submitted?
are you are trying to set the value of is_last after save?
according to code, it will work if the office_in_time value is change.

Try to set the values in function directly, instead of returning.

just call your function here, and check the trigger, this script will work when is_late value is changed. i think it should be in or out time.

yes, the document is submitted.But i want to save the value in the is_late field before saving the or submitting the record.

I am posting the whole screen shot of my script altogether serial by serial. See if you guys can help me. Sorry, i might irritating you.

Please review it guys.

frm.set_value("is_late", "Yes");
instead offrm.doc.is_late = "Yes";