Validate yes/no in repeatable and set status

Validates yes/no responses in repeatable sections and nested sections within repeatable sections, then sets the status for the record.

This example will loop through yes/no fields within repeatable sections. If no is the choice made on any yes/no field within the repeatable section then the status of the record is set to no, otherwise the status is set to yes.

ON('change', 'repeatable_section', function (event) {

  var yesNoFields = DATANAMES('YesNoField');
  var sectionFields = DATANAMES('Section');
  var repeatableFields = DATANAMES('Repeatable');
  var answersArray = [];
  var repeatableFieldNames = [];
  var insideRepeatableSectionFieldNames = [];
  var insideRepeatableYesNoFieldNames = [];
  var yesNoInsideSectionInsideRepeatableFieldNames = [];
  var sectionFieldNames = [];
  var yesNoInsideSectionFieldNames = [];

  if(repeatableFields != undefined){
  for(var a = 0; a < repeatableFields.length; a++){
       repeatableFieldNames.push(FIELDNAMES(repeatableFields[a]));
  }
    var flatRepeatableFieldNames = FLATTEN(repeatableFieldNames);
  }


  if(flatRepeatableFieldNames != undefined){
  for(var c = 0; c < flatRepeatableFieldNames.length; c++){
    if(FIELDTYPE(flatRepeatableFieldNames[c]) == 'Section')
      insideRepeatableSectionFieldNames.push(FIELDNAMES(flatRepeatableFieldNames[c]));
    if(FIELDTYPE(flatRepeatableFieldNames[c]) == 'YesNoField')
      insideRepeatableYesNoFieldNames.push(flatRepeatableFieldNames[c]);
  }
    var flatInsideRepeatableSectionFieldNames = FLATTEN(insideRepeatableSectionFieldNames);
    var flatInsideRepeatableYesNoFieldNames = FLATTEN(insideRepeatableYesNoFieldNames);
  }

  if(flatInsideRepeatableSectionFieldNames != undefined){
  for(var d = 0; d < flatInsideRepeatableSectionFieldNames.length; d++){
    if(FIELDTYPE(flatInsideRepeatableSectionFieldNames[d]) == 'YesNoField')
      yesNoInsideSectionInsideRepeatableFieldNames.push(flatInsideRepeatableSectionFieldNames[d]);
  }
    var flatYesNoInsideSectionInsideRepeatableFieldNames = FLATTEN(yesNoInsideSectionInsideRepeatableFieldNames);
  }

  if(sectionFields != undefined){
  for(var b = 0; b < sectionFields.length; b++){
       sectionFieldNames.push(FIELDNAMES(sectionFields[b]));
  }
    var flatSectionFieldNames = FLATTEN(sectionFieldNames);
  }

      var nestedYesNoFields = FLATTEN(flatYesNoInsideSectionInsideRepeatableFieldNames, flatInsideRepeatableYesNoFieldNames);


    if(repeatableFields != undefined && nestedYesNoFields != undefined) {
    for(var f = 0; f < repeatableFields.length; f++){
      for(var g = 0; g < nestedYesNoFields.length; g++){
      answersArray.push(REPEATABLEVALUES(VALUE(repeatableFields[f]), nestedYesNoFields[g]));
    }
  }
    }


   if(flatSectionFieldNames != undefined){
  for(var e = 0; e < flatSectionFieldNames.length; e++){
    if(FIELDTYPE(flatSectionFieldNames[e]) == 'YesNoField')
     yesNoInsideSectionFieldNames.push(FIELDNAMES(sectionFieldNames[e]));
  }
     var flatYesNoInsideSectionFieldNames = FLATTEN(yesNoInsideSectionFieldNames);
   }

  if(flatYesNoInsideSectionFieldNames != undefined){
  for(var h = 0; h < flatYesNoInsideSectionFieldNames.length; h++){
    answersArray.push(VALUE(flatYesNoInsideSectionFieldNames[h]));
  }
  }

  if(yesNoFields != undefined){
  for(var k = 0; k < yesNoFields.length; k++) {
       answersArray.push(VALUE(yesNoFields[k]));
  }
  }

  if(answersArray != undefined){
  var finalArray = FLATTEN(answersArray);
  }

  if(CONTAINS(finalArray, 'no'))
     SETSTATUS('no');
  else
    SETSTATUS('yes');

});