Calculate hours in related app based on category
Hello,   I am trying to calculate hours based on the category of a related app.  Why isn't this working?  It's still pulling the total hours and not just the ones for Expensify.   var cat = @All of Task Code;var time = @All of Hours Worked;var sum = 0;for(var i = 0; i < cat.length; i++){if(cat[i] = "Expensify"){sum += time[i];}};sum   Any help would be great!

  • Hi Janiece, I assume, the field  "billable/nonbillable" is in the same app as "Task Code".  You only need to add [i] behind status in the if condition - and keep in mind to use == not = for comparisons. {if(cat[i] == "Expensify" && status[i] == "Billable") One tip: For comparisons with multi-option categories you can use: if(cat[i].indexOf("Expensify") > -1) instead of == Yes, you can use relationship fields as criteria - but that depends on how you want to use it and in most cases it needs a workaround.   Rainer  
  • Hi Rainer, First let me say I have learned a ton reading through your answers to peoples posts and questions.  Thank you so much for that. That said, the above calculation does actually work.  But if I try to use 2 or more conditions, they don't.  I get an odd result where it starts taking hours away from the other conditional fields. var cat = @All of Task Code;var time = @All of Hours Worked; var status = @all of billable/nonbillable;var sum = 0;for(var i = 0; i < cat.length; i++){if(cat[i] = "Expensify" && status == "Billable"){sum += time[i];}};sum The result is always weird calculations that aren't correct.  The category field is multi-optional but only one option is ever selected.  It is setup this way due to webform criteria. Status field is single choice category. I've made things work the way they are but it would be really helpful to be able to use two different fields for criteria and have it work. Also, is there anyway to use a relationship field as criteria? Thank you again for all of your help. Janiece  
  • Hi Janiece, the code seems to be correct.  What doesn't work? Do you get an error notification in the preview? Is it the same field "Task Code" and "Hours Worked" as in the first calculation field (the one which worked)?   Is "Task Code" a multi choice or a single choice category field?  Rainer
  • Hi Rainer, Thank you.  It worked for one field but when I tried to do it again for any other ones, it doesn't work.  Any idea why? var cat = @All of Task Code; var time = @All of Hours Worked;var sum = 0;for (var i = 0; i < cat.length; i++){if (cat[i] == "Accounting"){sum += time[i]}};sum
  • Hi Janiece, it doesn't work cause there's a mistake in you code For comparisons you've to use double equal signs. if(cat[i] == "Expensify") Equal to: ==Not equal: != Also if you want to concatenate 2 (or more) conditions you need double signs, e.g.: if(cat[i] == "Expensify" && time[i] > 10) AND: &&OR: || Rainer
This question is closed.