Execute custom Client Script directly during Mark Attendance Button

I created new custom fields in the Attendance Doctype to do some calculations for minutes and lates
But when i press Mark Attendance Button from Shift Type DocType, it creates and submit all the records
and my script is not executed
But when entering each record , it is executed

Is there a way to automatically commit changes to all records?

** my site is in frappe.cloud **

@Ahmed_Alshehari I just gave it a try . code executed just fine.

1 Like

@bahaou

This is the beginning of my script

frappe.ui.form.on('Attendance', {
     setup: function (frm) {

and nothing happens
but when i chagne ā€œsetupā€ to ā€œonloadā€ itā€™s work just when i onload the record

Hi @Ahmed_Alshehari,

As per my knowledge,
setup event is triggered only when the form is initially loaded, while the onload event is triggered both when the form is initially loaded and when itā€™s reloaded or changes are made.

So, if you want your custom script to execute only when the form is first loaded, use setup. If you want it to execute when the form is initially loaded and whenever changes are made to the form, use onload.

More details for check it.

Reference: Form Scripts

Thank You!

1 Like

@Ahmed_Alshehari you better use server scripts to do your calculations.

2 Likes

Hello,

Instead of Client Script try Server Script on event Before Insert.

1 Like

i have tried all events ( before_insert, after_insert, on_submit, onload, ā€¦ etc )
and nothing happens, and onload works only when i open any attendance record

I want to calculate time between (In Time) and (Start Time)
In time is a native field in attendance
Start time is a custom field to fetch value from Shift Type Doctype

@bahaou
@Rehan_Ansari

thanks for the reply
i used custom client script to do the calculation
but it didnā€™t work when attendance is created.

I want to calculate time between (In Time) and (Start Time)
In time is a native field in attendance (datetime)
Start time is a custom field to fetch value from Shift Type Doctype (time)

i tried with server script and it is execited with mark attendance button
and i get time defferent between (in_time, out time) or ( start_time, end_time) when i try it
but donā€™t work with (start_time, custom_in_time_wd) and get error

#this code to delete Date from  (in_time, out_time) to only time fields (in_time_wd, out_time_wd)
in_t = frappe.utils.get_datetime(doc.in_time)
doc.custom_in_time_wd = frappe.utils.get_time(in_t)
out_t = frappe.utils.get_datetime(doc.out_time)
doc.custom_out_time_wd = frappe.utils.get_time(out_t)


#this code to calculate the difference between custom_in_time_wd and start time 
strt_time=frappe.utils.get_datetime(doc.start_time)
in_time_wd=frappe.utils.get_datetime(doc.custom_in_time_wd)
diff=frappe.utils.time_diff_in_seconds(in_time_wd,strt_time) 
doc.custom_test_late_int=diff

but when i press mark attendance i get this error

" AttributeError: ā€˜datetime.datetimeā€™ object has no attribute ā€˜total_secondsā€™ "

Did you find any solution for this @Ahmed_Alshehari ?