Problem with multiple 'for each' statements in workflow

Problem with multiple 'for each' statements in workflow

New to ZC and almost a novice at coding. I'm mostly getting along fine learning Deluge through the documentation and googling, but I'm completely stuck on this one. At line 22 I get the error 'Variable Actual_Work_Time does not exist in Activity_log'. I've tried every way I can think of to specify that 'Actual_Work_Time' is in the form Projects (not the subform Activity_log) but it won't accept any.

Forms/fields (only for as far as the problematic line):
  1. Projects (fields Total_Project_Time_Actual)
    1. subform Tasks (fields Live_for_Logging, Actual_Work_Time)
      1. subform Activity_log (fields Start_Logging, End_Logging, Total_Time)
The problematic line is supposed to sum the Actual_Work_Time fields in the Tasks subform and input the result in the Total_Project_Time_Actual field of the Projects form.

The code works except for line 22 (it runs fine if I comment the line out).

It runs on successful form submission of the Activity_Log form (the variant capitalisation is correct - it's Activity_Log as a form but Activity_log when it's a subform of Tasks).

Any help greatly appreciated!

  1. task = Tasks[ID = input.Task_ID];
  2. project = Projects[ID = task.Project];
  3. if(Start_Logging = "Yes")
  4. {
  5. input.Start_Logging = "No";
  6. openURL("#Script.page.close","same window");
  7. openURL("#Page:Timekeeping","same window");
  8. }
  9. if(End_Logging = "Yes")
  10. {
  11. task.Live_for_Logging="No";
  12. totaltask = 0;
  13. for each  r in Activity_log[Task_ID == task.ID]
  14. {
  15. totaltask = totaltask + r.Total_Time;
  16. }
  17. task.Actual_Work_Time=totaltask;
  18. totalproject = 0;
  19. for each  r in Tasks[Project == project.ID]
  20. {
  21. totalproject = totalproject + r.Actual_Work_Time;
  22. }
  23. project.Total_Project_Time_Actual=totalproject;
  24. task.Actual_Fee_Per_Hour=task.Actual_Fee / task.Actual_Work_Time;
  25. taskvalueactual = task.Actual_Fee;
  26. tasktimeactual = task.Actual_Work_Time;
  27. for each  r in Activity_log
  28. {
  29. logvalueactual = taskvalueactual / tasktimeactual * input.Total_Time;
  30. }
  31. input.Log_Value_Actual_Fee = logvalueactual;
  32. taskvalueest = task.Estimated_Fee;
  33. tasktimeest = task.Estimated_Work_Time;
  34. if(task.Estimated_Fee = 0 || task.Estimated_Fee = null || task.Estimated_Work_Time = 0 || task.Estimated_Work_Time = null)
  35. {
  36. //do nothing
  37. }
  38. else
  39. {
  40. for each  r in Activity_log
  41. {
  42. logvalueest = taskvalueest / tasktimeest * input.Total_Time;
  43. }
  44. }
  45. input.Log_Value_Estimated_Fee = logvalueest;
  46. input.End_Logging = "No";
  47. openURL("#Script.page.close","same window");
  48. openURL("#Page:Timekeeping","same window");
  49. }