Child task hours needs to calculated on Parent task

Hello, Everyone!
I want Actual hours spent to parent task from all child tasks.

If anyone have any idea. Please help!
Thanks in advance!

Hi @Amit_Prajapati

can you explain more? are you want to calculate specific field from child table in Parent table ?

Hi @Omar_Mohammed

Thanks for the reply,

For example,
There is one parent task (is group)
and below there are 5 dependent tasks that have different actual time hours and
i want all that dependent hours actual hours total in the Parent taskā€™s actual time.

see attached image.

Thanks

run a loop on your child table field, add all values,use set value to put in the desired field on parent. use appropriate triggers ( on save, submit, field change in child doc etc )

@Amit_Prajapati as @neerajvkn say just use for loop in child table, this code is help you

// calculate Actual Time of Parent Task
frappe.ui.form.on('ChildTableName', {
	"child_table_field_actual_time":function(frm) {
		let amount = 0;
		let items = frm.doc.dependecies;
		for(let i in items){
		    amount += frm.doc.dependecies[i].actual_time;
		}
		frm.set_value("parent_table_field_actual_time", (amount));
	}
});

// updating on Actual Time of Parent Task when remove row of child table
frappe.ui.form.on('ChildTableName', {
	dependecies_remove:function(frm) {
		let amount = 0;
		let items = frm.doc.dependecies;
		for(let i in items){
		    amount += frm.doc.dependecies[i].actual_time;
		}
		frm.set_value("parent_table_field_actual_time", amount);
	}
});

I hope is clean example

Thanks
Omar Mohammed

@Amit_Prajapati This is just a simplified version of @Omar_Mohammed code:

function updateActualTime(frm) {
	let amount = 0;
	let items = frm.doc.dependecies;
	for (let i in items){
	    amount += frm.doc.dependecies[i].actual_time;
	}
	frm.set_value("parent_table_field_actual_time", amount);
}
frappe.ui.form.on('ChildTableName', {
    // calculate Actual Time of Parent Task
	"child_table_field_actual_time": updateActualTime,
    // updating on Actual Time of Parent Task when remove row of child table
	dependecies_remove: updateActualTime
});

I hope that things are clear for you now.

2 Likes

Thank you for the responseā€¦

I am new to ERP and can you please let us know where do I have to put this code?

Thank you.

You can put these code in client script doctype.

Read this for more detail.
Client Script (frappeframework.com)

@kid1194 @Omar_Mohammed
Thank you for the response!

We have applied this script to a task, but it is not working. Please find below the script that we have on our platform.

frappe.ui.form.on(ā€˜Taskā€™, {
refresh(frm) {
frm.add_fetch(ā€œprojectā€,ā€œproject_codeā€,ā€œproject_codeā€);
}
});
function updateActualTime(frm) {
let amount = 0;
let items = frm.doc.dependecies;
for (let i in items){
amount += frm.doc.dependecies[i].actual_time;
}
frm.set_value(ā€œparent_table_field_actual_timeā€, amount);
}
frappe.ui.form.on(ā€˜ChildTableNameā€™, {
// calculate Actual Time of Parent Task
ā€œchild_table_field_actual_timeā€: updateActualTime,
// updating on Actual Time of Parent Task when remove row of child table
dependecies_remove: updateActualTime
});

We have already applied one script to a task for our usage and after that, we have added your code, but somehow itā€™s not working.

Can you please take a look and help us!

@Pheakdey_Tes1 Thank you for the response!

Thank you.

@Amit_Prajapati The code that was presented was meant to explain the process.
If you are using the Project > Task form without any customization, then I believe that this code will work for you. Sorry Iā€™m unable to test it myself at the moment.

// Your code
frappe.ui.form.on(ā€˜Taskā€™, {
    refresh(frm) {
        frm.add_fetch(ā€œprojectā€,ā€œproject_codeā€,ā€œproject_codeā€);
    }
});
function updateActualTime(frm) {
    let amount = 0,
    items = frm.doc.depends_on,
    i;
    for (i in items) {
        amount += items[i].actual_time;
    }
    frm.set_value(ā€œactual_timeā€, amount);
}
frappe.ui.form.on(ā€˜Taskā€™, {
    // calculate Actual Time of Parent Task
    actual_time: updateActualTime
});
frappe.ui.form.on(ā€˜Task Depends Onā€™, {
    // updating on Actual Time of Parent Task when remove row of child table
    depends_on_add: updateActualTime,
    depends_on_remove: updateActualTime
});
1 Like