Select to view content in your preferred language

Loop through and return in a list records from a survey123 repeat question using advanced formatting

700
5
Jump to solution
05-29-2024 07:04 AM
dwold
by
Frequent Contributor

I have a table with records from a repeat question in survey123. I am trying to loop through and return all associated records and display in a list.

Table:

dwold_0-1716991177347.png

My attempt to use advanced formatting to get this to work:

// Initialize empty strings to store results
var problemResults = "";
var solutionResults = "";

// Loop through capability_target_problem array
for (var i in $datapoint.capability_target_problem) {
    problemResults += $datapoint.capability_target_problem[i] + ", ";
}

// Loop through capability_target_solution array
for (var j in $datapoint.capability_target_solution) {
    solutionResults += $datapoint.capability_target_solution[j] + ", ";
}

// Remove the trailing comma and space from the result strings
problemResults = Trim(Left(problemResults, Count(problemResults) - 2));
solutionResults = Trim(Left(solutionResults, Count(solutionResults) - 2));

// Combine the results into a single string or return as separate values
var combinedResults = "Problems: " + problemResults + " | Solutions: " + solutionResults;

// Return the combined results
return combinedResults;

return {
  textColor: '',
  backgroundColor: '',
  separatorColor:'',
  selectionColor: '',
  selectionTextColor: '',
   attributes: {
     combinedResults
   }
}

Line item:

dwold_1-1716991273960.png

Results = empty list:

dwold_3-1716991407510.png

 

My goal is to get all 4 problem and solutions shown above records to display

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Based on the fields and the filter, I'm confused how it's only giving you one. It should be as simple as setting up the filter.

That said, when you're configuring a widget, you should get to preview the widget in its unfiltered state. The fact that you're only seeing one even on the configuration screen is strange. Is it as simple as the "maximum features" setting?

jcarlson_0-1716997523792.png

Or perhaps there's a filter on the widget?

- Josh Carlson
Kendall County GIS

View solution in original post

5 Replies
jcarlson
MVP Esteemed Contributor

Advanced Formatting evaluates per-feature. It won't really have a way of accessing related data or anything. You say "loop through array", but unless the field itself has an array in it (not impossible, but I've never seen anyone actually do it), there won't be anything to loop through.

Couldn't you just reference the fields directly in the item template?

- Josh Carlson
Kendall County GIS
dwold
by
Frequent Contributor

@jcarlson thanks for the feedback, Josh! I was probably going at this the wrong way and didn't explain my workflow in enough detail. I have a table populated within a dashboard with records from a Survey123 form. In that form, there is a repeat question creating a related table with the problem/solution responses I would like to display in the list. Within the table, I have a filter established to populate the list displaying all the problems/solutions (could be 0-30) associated with that selected record.

Table:

dwold_0-1716994756635.pngdwold_1-1716994774302.png

Filter:

dwold_2-1716994810727.png

Current list (returns only first problem/solution)

dwold_3-1716994942948.png

Current list configuration:

dwold_5-1716995116722.png

Repeat table:

dwold_4-1716994980838.png

My desired outcome would be to get all 4 of the related Problems/Solutions to display in the list from the selected record in the table

0 Kudos
jcarlson
MVP Esteemed Contributor

Based on the fields and the filter, I'm confused how it's only giving you one. It should be as simple as setting up the filter.

That said, when you're configuring a widget, you should get to preview the widget in its unfiltered state. The fact that you're only seeing one even on the configuration screen is strange. Is it as simple as the "maximum features" setting?

jcarlson_0-1716997523792.png

Or perhaps there's a filter on the widget?

- Josh Carlson
Kendall County GIS
dwold
by
Frequent Contributor

@jcarlson that was it! I had  Maximum features displayed set to 1. I bumped to 100 and they all display. I should have caught that. Thanks again for your help with this, it is always appreciated!

0 Kudos
leahmaps
Frequent Contributor

I am actually glad I saw this as I have a similar situation I am trying to work through (here is my post, but I will do a TLDR here, too).

Basically, I want to do a loop of any multiple choice selected answer from a survey123. The issue is that they get submitted like this:

leahmaps_0-1717013283322.png

Which A) I need spaces and B) makes it difficult to create a list. Would using some similar syntax to what you have above (for var x +=) work for something like this? I am still learning more advanced arcade expressions so looping and adding to a list is something new for me!

 

0 Kudos