Select to view content in your preferred language

Map Viewer popup: Confitional popup? change popup based on one data

344
4
Jump to solution
4 weeks ago
YukoYokozawa
Frequent Contributor

Hi, I have two different set of fields and want to switch them based on one data. Is this possible?

0 Kudos
2 Solutions

Accepted Solutions
KenBuja
MVP Esteemed Contributor

When posting code, please use the "Insert/edit code sample" button. It makes reading your code much easier.

I don't know why it's returning empty attributes for those fields. Have you checked the FeatureSet to see if there are records with no values in the attributes?

The code returns the fields as they are ordered in the FeatureSet. To use a custom order, use this code

var fields = Schema($feature).fields;
var attributes = {};
var fieldInfos = [];

var fieldList;
if ($feature.UNIT_PI_AF == "Single") {
  fieldList = ["Site ID", "Unit PI AF", "Pressure URL", "Battery URL", "P1 Pressure", "P1 High High Alarm", "P1 High Alarm", "P1 High Alarm (Derived)", "P1 Low Alarm", "P1 Low Alarm (Derived)", "P1 Low Low Alarm", "Last Update PI"];
} else {
  fieldList = ["Site ID", "Unit PI AF", "Pressure URL", "Battery URL", "P1 Pressure", "P1 High High Alarm", "P1 High Alarm", "P1 High Alarm (Derived)", "P1 Low Alarm", "P1 Low Alarm (Derived)", "P1 Low Low Alarm", "P2 Pressure", "P2 High High Alarm", "P2 High Alarm", "P2 High Alarm (Derived)", "P2 Low Alarm", "P2 Low Alarm (Derived)", "P2 Low Low Alarm", "Last Update PI"];
}

for (var j of fieldList) {
  for (var i in fields) {
    if (fields[i].name == j || fields[i].alias == j) {
      attributes[fields[i].alias] = $feature[fields[i].name];
      Push(fieldInfos, { fieldName: fields[i].alias });
    }
  }
}

return { type: "fields", fieldInfos: fieldInfos, attributes: attributes };

 

View solution in original post

0 Kudos
YukoYokozawa
Frequent Contributor

Hi! Thanks for your reply. With your code samples, I made some progress but not be able to complete by myself. One of my team members helped me complete the code, and it's now working. Thank you very much for your help. Here is the code that working now.

var fields = Schema($feature).fields;
var fieldInfos = []
var sortedFieldInfos = []
var attributes = {}
var unitPIAF = $feature.UNIT_PI_AF
var visibleFields = ["SITEID", "UNIT_PI_AF", "PRESSURE_URL", "BATTERY_VOLT_URL", "P1_PRESSURE", "P1_HIHI_ALARM", 
    "P1_HI_ALARM", "P1_HI_ALARM_DERIVED", "P1_LO_ALARM", "P1_LO_ALARM_DERIVED", "P1_LOLO_ALARM", "LAST_UPDATE_PI"]

if(unitPIAF != "Single") {
  visibleFields = ["SITEID", "UNIT_PI_AF", "PRESSURE_URL", "BATTERY_VOLT_URL", "P1_PRESSURE", "P1_HIHI_ALARM", 
    "P1_HI_ALARM", "P1_HI_ALARM_DERIVED", "P1_LO_ALARM", "P1_LO_ALARM_DERIVED", "P1_LOLO_ALARM", "P2_PRESSURE", "P2_HIHI_ALARM", 
    "P2_HI_ALARM", "P2_HI_ALARM_DERIVED", "P2_LO_ALARM", "P2_LO_ALARM_DERIVED", "P2_LOLO_ALARM", "LAST_UPDATE_PI"]
} 
for (var i in fields) {
  var field = fields[i]
  var fieldName = field.name
  var fieldAlias = field.alias
  if (Includes(visibleFields, fieldName)) {
    Push(fieldInfos, {fieldName: fieldName, label: fieldAlias})
    if (field.type == "esriFieldTypeDate") {
      attributes[fieldName] = Text($feature[fieldName], 'MM/DD/YYYY hh:mm:ss')
    } else {
      attributes[fieldName] = $feature[fieldName]
    }
  }
}
for (var j in visibleFields) {
  for (var k in fieldInfos) {
    if (fieldInfos[k].fieldName == visibleFields[j]) {
      push(sortedFieldInfos, fieldInfos[k])
    }
  }
}

return {
    type: 'fields',
    fieldInfos:  sortedFieldInfos,
    attributes: attributes
}

View solution in original post

4 Replies
KenBuja
MVP Esteemed Contributor

You can use an Arcade element to do this.

In this example I am testing whether a feature has the attribute "Fish and Benthic" (line 14). This field has a domain, so I have to get the Domain Name ( line 12). If the feature does have that attribute, I want the display to contain the five fields in the line 16. Otherwise, it will contain the three fields in line 22.

var fields = Schema($feature).fields;
var attributes = {};
var fieldInfos = [];

function fieldTest(fieldList, fieldName, fieldAlias) {
  if (Includes(fieldList, fieldAlias)) {
    attributes[fieldalias] = $feature[fieldName];
    Push(fieldInfos, { fieldName: fieldAlias });
  }
}

var code = DomainName($feature, "Survey");
for (var i in fields) {
  if (code == "Fish and Benthic")
    fieldTest(
      ["Site ID", "Fish Status", "Fish Date", "Benthic Status", "Benthic Date"], //the list of fields you'd like if the feature has this attribute
      fields[i].name,
      fields[i].Alias
    );
  else
    fieldTest(
      ["Site ID", "Fish Status", "Fish Date"], //the list of fields you'd like if the feature doesn't have that attribute
      fields[i].name,
      fields[i].alias
    );
}

return { type: "fields", fieldInfos: fieldInfos, attributes: attributes };

 These are what the two popups look like

2025-09-10_14-57-12.PNG

2025-09-10_14-57-28.PNG

YukoYokozawa
Frequent Contributor

Thanks for reply! I tried your code and was able to show two separate popups by data like your sample. But the data don't show up... And the field orders are not what I want.

YukoYokozawa_0-1757625806496.png

I'm using Uni PI AF for the condition, and if it's "Single", then this popup without P2 show up, and other than single, this popup show up. So, the single and dual is the only data show up nothing else. And the number in the pink circle is the order I want to see in the table. Attached the code I used. 

0 Kudos
KenBuja
MVP Esteemed Contributor

When posting code, please use the "Insert/edit code sample" button. It makes reading your code much easier.

I don't know why it's returning empty attributes for those fields. Have you checked the FeatureSet to see if there are records with no values in the attributes?

The code returns the fields as they are ordered in the FeatureSet. To use a custom order, use this code

var fields = Schema($feature).fields;
var attributes = {};
var fieldInfos = [];

var fieldList;
if ($feature.UNIT_PI_AF == "Single") {
  fieldList = ["Site ID", "Unit PI AF", "Pressure URL", "Battery URL", "P1 Pressure", "P1 High High Alarm", "P1 High Alarm", "P1 High Alarm (Derived)", "P1 Low Alarm", "P1 Low Alarm (Derived)", "P1 Low Low Alarm", "Last Update PI"];
} else {
  fieldList = ["Site ID", "Unit PI AF", "Pressure URL", "Battery URL", "P1 Pressure", "P1 High High Alarm", "P1 High Alarm", "P1 High Alarm (Derived)", "P1 Low Alarm", "P1 Low Alarm (Derived)", "P1 Low Low Alarm", "P2 Pressure", "P2 High High Alarm", "P2 High Alarm", "P2 High Alarm (Derived)", "P2 Low Alarm", "P2 Low Alarm (Derived)", "P2 Low Low Alarm", "Last Update PI"];
}

for (var j of fieldList) {
  for (var i in fields) {
    if (fields[i].name == j || fields[i].alias == j) {
      attributes[fields[i].alias] = $feature[fields[i].name];
      Push(fieldInfos, { fieldName: fields[i].alias });
    }
  }
}

return { type: "fields", fieldInfos: fieldInfos, attributes: attributes };

 

0 Kudos
YukoYokozawa
Frequent Contributor

Hi! Thanks for your reply. With your code samples, I made some progress but not be able to complete by myself. One of my team members helped me complete the code, and it's now working. Thank you very much for your help. Here is the code that working now.

var fields = Schema($feature).fields;
var fieldInfos = []
var sortedFieldInfos = []
var attributes = {}
var unitPIAF = $feature.UNIT_PI_AF
var visibleFields = ["SITEID", "UNIT_PI_AF", "PRESSURE_URL", "BATTERY_VOLT_URL", "P1_PRESSURE", "P1_HIHI_ALARM", 
    "P1_HI_ALARM", "P1_HI_ALARM_DERIVED", "P1_LO_ALARM", "P1_LO_ALARM_DERIVED", "P1_LOLO_ALARM", "LAST_UPDATE_PI"]

if(unitPIAF != "Single") {
  visibleFields = ["SITEID", "UNIT_PI_AF", "PRESSURE_URL", "BATTERY_VOLT_URL", "P1_PRESSURE", "P1_HIHI_ALARM", 
    "P1_HI_ALARM", "P1_HI_ALARM_DERIVED", "P1_LO_ALARM", "P1_LO_ALARM_DERIVED", "P1_LOLO_ALARM", "P2_PRESSURE", "P2_HIHI_ALARM", 
    "P2_HI_ALARM", "P2_HI_ALARM_DERIVED", "P2_LO_ALARM", "P2_LO_ALARM_DERIVED", "P2_LOLO_ALARM", "LAST_UPDATE_PI"]
} 
for (var i in fields) {
  var field = fields[i]
  var fieldName = field.name
  var fieldAlias = field.alias
  if (Includes(visibleFields, fieldName)) {
    Push(fieldInfos, {fieldName: fieldName, label: fieldAlias})
    if (field.type == "esriFieldTypeDate") {
      attributes[fieldName] = Text($feature[fieldName], 'MM/DD/YYYY hh:mm:ss')
    } else {
      attributes[fieldName] = $feature[fieldName]
    }
  }
}
for (var j in visibleFields) {
  for (var k in fieldInfos) {
    if (fieldInfos[k].fieldName == visibleFields[j]) {
      push(sortedFieldInfos, fieldInfos[k])
    }
  }
}

return {
    type: 'fields',
    fieldInfos:  sortedFieldInfos,
    attributes: attributes
}