Can I add a conditional statement in Arcade to display one of two FeatureSet script values?

5247
26
01-28-2020 08:29 AM
AdamLevine
New Contributor

I successfully used https://community.esri.com/community/gis/web-gis/arcgisonline/blog/2018/12/10/overlapping-features-i... in two separate scripts to combine two surveys with the same feature layer points.  I am getting the results I desire separately.   So now I want to add a third piece and that is a conditional - IF A doesn't exist then return B else return A.  

However I have some code and it is trying to return the entire feature class and I can't seem to pull just one field from the feature class.   (see Script 3)  NOTE: I am relatively new to Arcade so I may be going about this all wrong.

Here are the two scripts that are pulling the info I want:

Script 1: var intersectLayer =Intersects(FeatureSetByName($map,"BowersTransactions"), $feature)

for (var f in intersectLayer){
return f.feno2
}

Script 2: var intersectLayer =Intersects(FeatureSetByName($map,"BowersInspections"), $feature)

for (var f in intersectLayer){
return f.extinguisherno
}

Script 3: var inspfe =Intersects(FeatureSetByName($map,"BowersInspections"), $feature)
var transfe =Intersects(FeatureSetByName($map,"BowersTransactions"), $feature)

if (isEmpty(transfe))
{return inspfe;}
else {return transfe;}

Script 3 doesn't error out it tries to display all 20 or so fields.  So it won't allow me to add it to a popup since it is too much information.   I included the result from Script 3.

I thought briefly that FeatureSetByID might narrow this down to one field which is what i want however the little bit of reading I have done on that function seems to indicate that it uses arrays and I am just trying to pull the data from a field if it exists. 

Tags (1)
0 Kudos
26 Replies
XanderBakker
Esri Esteemed Contributor

Hi Adam Levine 

To give you an idea of what the expression would look like:

var group_id = $feature["NameOfGroupField"];
var sql = "NameOfGroupField = '" + group_id + "'"; 
var records = OrderBy(Filter($layer, sql), "adjdate DES");
var cnt = Count(records);

var result = ""; // or fill with info when there is no data
if (cnt > 0) {
    var info = First(records);
    result = info.NameOfFieldWithInfoToShow;
}

return result;

The if statement is redundant because in the same layer you will always have at least 1 result.

0 Kudos
AdamLevine
New Contributor

Xander,

Can I use some of the 'hidden' fields that are inherent  with Survey123 for instance EditDate, and ObjectID or do I have to rely on the fields that I defined for the survey?   Also, I have a filter on Location ID so that only those that pertain to the building in question are shown.  So, Location ID would have to show up based on the filter if that can still be used as the group identification?  

Adam

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Adam Levine ,

Every field that is exposed in the layer should be available for use in the Arcade expression. 

You can use the LocationID for filtering the data. I was just wondering if that was the field, since in the example records you showed the field is empty.

0 Kudos
AdamLevine
New Contributor

Xander,

The 'group identification' would be LocationID.    The screenshot that I sent you was my mistake.   The LocationID wouldn't be blank on my map ever.

The code below is VERY close to what I need:  (the only problem is if I submit another Transaction for that locationID then it is pulling the first one (that would be the 'first' piece of code correct?)   IF I manually change the filter to an updated date range for the BowersTransactions featureset then this code does bring up the correct FE#)   So, I need to figure out how to bring up the last result in the array, instead of the first.......am I understanding that correctly? 

var result = "";
var transfe =Intersects(FeatureSetByName($map,"BowersTransactions"), $feature);
if (Count(transfe)==0) {
    // no BowersTransactions
    var inspfe =Intersects(FeatureSetByName($map,"BowersInspections"), $feature);
    if (Count(inspfe)==0){
        // BowersInspections either
        result = "No results found";
    } else {
        // just BowersInspections
        result = First(inspfe).extinguisherno;
    }
} else {
    // BowersTransactions found, don't check BowersInspections
    result = First(transfe).feno2;
}

return result;

Thanks,

Adam

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Adam Levine ,

In the example I posted earlier ("on Feb 1, 2020 8:33 AM"), line 3 contains and OrderBy specifying in this case that it will order the results using "adjdate DES" (order it on the field adjdate in descending order). If I do that and use the First, this will take the most recent record. In case you would change it to "adjdate ASC" and again use First, this will get the oldest record. So I think you are missing the OrderBy to assure you get the record you need to have. 

0 Kudos
AdamLevine
New Contributor

Xander,

Thanks again - I was looking at the post from Feb 1 and the code and your explanation make sense - I am just confused by the way I have things set up in this map so I am still trying to figure this out.   Would I be able to get further assistance (screen share) from support or is this something that wouldn't be supported by the ESRI support call center?  

Thanks,

Adam

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Adam Levine ,

I am not sure if that is part of what is called support. In Colombia, the client would normally reach out to the sales executive or directly solution engineer to get some extra help with implementing these things. Do you have contact with your sales executive or solution engineer? If not, you could share the map and layer to a group and invite my ArcGIS Online user  ("xbakker.spx") to that group and I could have a look if you want that and you are able to share the content with me .

0 Kudos
AdamLevine
New Contributor

Xander,

I invited you to a group called ESRI Support in our organization and gave you access to the stuff I am working on.  

Thanks again,

Adam

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Adam Levine ,

Thanks for sharing the content. I received an error on the basemap not being available. Are you using a personalized basemap in the web map? If so, can you save a copy of the web map with a standard basemap? The error is causing the other layer(s) not to load and therefore I can't see any configurations (expressions) you may have applied to the layers. 

If this is not the case, I will load the layers and revise the expression you have up to now and try and adapt it based on the data.

0 Kudos
AdamLevine
New Contributor

Xander,

Give it one more shot and then I will do it without the basemap if necessary. 

Thanks,

Adam

0 Kudos