portalId = 60XXXXXX;
// projectId = 19063XXXXXXX;
// taskid = 19063XXXXXXX;
response = zoho.projects.getRecordById(portalId,projectId,"tasks",taskid,"bs");
// info response;
tasks = response.get("tasks").get(0);
info tasks.get("id");
p_task_id = tasks.get("parent_task_id");
info "task_parent" + p_task_id;
custom_fields = tasks.get("custom_fields");
for each custom_field in custom_fields
{
if(custom_field.get("label_name") == "Actual_Cost")
{
task_amount_act = ifnull(custom_field.get("value").toDecimal(),0);
// Convert value to decimal
// actual_cost_l1 = actual_cost_l1 + task_actual_cost;
info "task_amount_act >>" + task_amount_act;
}
}
p_task_res = zoho.projects.getRecordById(portalId,projectId,"tasks",p_task_id,"bs");
tasks_p = p_task_res.get("tasks").get(0);
tasks_p_id = tasks_p.get("id");
info "P_t_id :" + tasks_p_id;
custom_fields_p = tasks_p.get("custom_fields");
for each custom_field in custom_fields_p
{
if(custom_field.get("label_name") == "Actual_Cost")
{
task_actual_cost = ifnull(custom_field.get("value").toDecimal(),0);
// Convert value to decimal
// actual_cost_l1 = actual_cost_l1 + task_actual_cost;
info "task_actual_cost >>" + task_actual_cost;
}
}
total_sum_st_l1 = task_actual_cost + task_amount_act;
total_sum_dis = total_sum_st_l1.toLong();
info "total_sum_st_l1>>" + total_sum_dis;
task_details = Map();
ts = Map();
ts.put("UDF_LONG10",total_sum_dis);
task_details.put("custom_fields",ts);
res = zoho.projects.update(portalId,projectId,"tasks",tasks_p_id,task_details,"bs");
info "res >>" + res;
return "";
This script is designed to aggregate the value of a custom field (e.g., "Actual Cost") from a subtask and add it to the corresponding custom field of its parent task. The workflow can be extended by implementing a rule that triggers this script whenever the subtask's custom field is updated.
Key Considerations:
- Single Update Application: This script is intended for scenarios where the custom field value is updated once. If the field is subject to frequent updates, additional logic may be required to ensure accurate aggregation.
- Custom Field Handling: When updating values in custom fields, it is crucial to verify the field type and format the input accordingly. You can determine the field type by inspecting the task's page.
Code Workflow:
Retrieve Subtask Details:
- Fetches the subtask details using
getRecordById
. - Extracts the custom field value for "Actual Cost."
Retrieve Parent Task Details:
- Fetches the parent task details using
getRecordById
. - Extracts the custom field value for "Actual Cost."
Calculate Total Cost:
- Sums the "Actual Cost" values from the subtask and the parent task.
Update Parent Task:
- Updates the parent task's custom field with the aggregated cost.
This script provides a foundational approach, allowing you to tailor the logic according to your specific requirements.