Calculating a descriptive field based off conditional loop merge | XM Community
Skip to main content

 Hello,
I have a request to set a calculated field using conditional logic and loop merge fields. Here is what I'm trying to accomplish with Javascript, and want to know if it is possible. I need to avoid using embedded data because it prohibits the user from going back in the form.
I'm trying to set a field in a survey to a field that is summed from a loop and merge section, if another condition is met within that loop and merge.
A conceptual look at what i'm trying to do, I have not tried to figure out what it would look like in javascript yet:
--------
Total direct costs = 0;
For (int i=0; i < 20 (my max loops); i++)
If (getSelectedAnswerValue = "Funded" in that loop merge round) {
Total direct costs += field that needs to be summed in loop and merge
Set descriptive field in survey to total direct costs variable
--------
Can something like this be done? Can I loop through loop and merge data and sum a field based off of a condition that is met within that loop and merge? Something like what piped text does, but I'm not sure if I can apply logic to piped text?

Any advice is greatly appreciated.

Thanks,
NAT

In the JS of the question where you want to display or capture total direct costs (after the loop), pipe all the costs and "Funded" answer values (I would use the recode instead of the text) from all the loops into an object (or two arrays if you prefer). Then loop through the object (or one of the arrays using the index to access the other array) to calculate total direct costs.
You can then update the html to display the total direct cost or update a question input value with the total direct cost.


Hi Tom,
I'm sorry for not responding sooner, as I appreciate your direction and was able to use it to come up with some code that is close to giving me what I need. Beginner js user here so sorry if I've missed anything obvious! but what I'm experiencing with the code below is that according to my console log there's one less name:value pair being accounted for in my for loop and therefore my sum.
Is my for loop the problem, or perhaps my object excludes a pair?
//sum up direct costs of "Funded" only grants
function sum(grants) {
    var sum = 0;

    for (var status in grants) {
      console.log(status);
//if status = 1 (funded recode val)
          if (status = 1) {
              sum += parseFloat( grants status] );
          }
        }
        return sum;
    }

//pipe all costs and answer values from all loops into object
var all_grants = { "${q://1_QID69/SelectedChoicesRecode}":"${q://1_QID70/ChoiceTextEntryValue}}", "${q://2_QID69/SelectedChoicesRecode}":"${q://2_QID70/ChoiceTextEntryValue}}",
      "${q://3_QID69/SelectedChoicesRecode}":"${q://3_QID70/ChoiceTextEntryValue}}", "${q://4_QID69/SelectedChoicesRecode}":"${q://4_QID70/ChoiceTextEntryValue}}",
      "${q://5_QID69/SelectedChoicesRecode}":"${q://5_QID70/ChoiceTextEntryValue}}", "${q://6_QID69/SelectedChoicesRecode}":"${q://6_QID70/ChoiceTextEntryValue}}",
      "${q://7_QID69/SelectedChoicesRecode}":"${q://7_QID70/ChoiceTextEntryValue}}", "${q://8_QID69/SelectedChoicesRecode}":"${q://8_QID70/ChoiceTextEntryValue}}",
      "${q://9_QID69/SelectedChoicesRecode}":"${q://9_QID70/ChoiceTextEntryValue}}", "${q://10_QID69/SelectedChoicesRecode}":"${q://10_QID70/ChoiceTextEntryValue}}",
      "${q://11_QID69/SelectedChoicesRecode}":"${q://11_QID70/ChoiceTextEntryValue}}", "${q://12_QID69/SelectedChoicesRecode}":"${q://12_QID70/ChoiceTextEntryValue}}",
      "${q://13_QID69/SelectedChoicesRecode}":"${q://13_QID70/ChoiceTextEntryValue}}", "${q://14_QID69/SelectedChoicesRecode}":"${q://14_QID70/ChoiceTextEntryValue}}",
      "${q://15_QID69/SelectedChoicesRecode}":"${q://15_QID70/ChoiceTextEntryValue}}", "${q://16_QID69/SelectedChoicesRecode}":"${q://16_QID70/ChoiceTextEntryValue}}",
      "${q://17_QID69/SelectedChoicesRecode}":"${q://17_QID70/ChoiceTextEntryValue}}", "${q://18_QID69/SelectedChoicesRecode}":"${q://18_QID70/ChoiceTextEntryValue}}",
      "${q://19_QID69/SelectedChoicesRecode}":"${q://19_QID70/ChoiceTextEntryValue}}", "${q://20_QID69/SelectedChoicesRecode}":"${q://20_QID70/ChoiceTextEntryValue}}"};

var funded_sum = sum(all_grants);
  
console.log("sum: " + funded_sum);
//set HTML of Total costs question   
jQuery("#QID75").html("  Total Costs: $" + funded_sum);

Thank you for your time! It's greatly appreciated.


Your object has duplicate keys. You can use an array of objects:
var all_grants = [
{rc:"${q://1_QID69/SelectedChoicesRecode}",val:"${q://1_QID70/ChoiceTextEntryValue}"},
{rc:"${q://2_QID69/SelectedChoicesRecode}",val:"${q://2_QID70/ChoiceTextEntryValue}"},
...
];



Got it, thank you! Below is what I came up with and it seems to be working! Appreciate your help.
  const allGrants =
    {rc:"${q://1_QID69/SelectedChoicesRecode}", val:"${q://1_QID70/ChoiceTextEntryValue}"}, 
    .....

  var sum = 0;

  allGrants.forEach((rc, index) => {
  if (allGrantsaindex].rc == 1) {
  sum += Number(rc.val);
  }
  });

  //set HTML 
  jQuery("#QID75").html("  Total Costs: $" + sum);


Leave a Reply