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

5244
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

Thanks adaml , that did the trick! I have access to the map and the layer and can see you have a number of Arcade expression already set up.

0 Kudos
AdamLevine
New Contributor

Xander,

Yes - I have a bunch of expressions going in that popup - the last one is the one I was working on expr4.  I submitted a test survey a few minutes ago for BW-228 - what you will see is expr2 brings up the FIRST transaction not the test that I just submitted a few mins ago - so that was what I was trying to get to show up for the expr4 script.   Does that make sense? 

Adam

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Adam Levine , 

I think I understand what you are trying to accomplish, but the data schema and the data posted causes some confusion.:

  • The expression you mentioned is configured on the layer called "Bowers - BuildingsSQL.DBO.BWR_FireEx".
  • The same expression is also configured on the layer "BowersTransactions"
  • You mentioned BW-228 which is stored in the field LocID, which should not be confused with the field LocationID (using alias CurrentLoc) used in the expression and which does not contain data for most records.
  • The test BW-228 can be found in both layers (at the same location) 
  • When testing first I used the layer BowersTransactions and just to get a result and test the expression I had to change field names, since for instance the adjdate field has the same date for multiple records, which does not allow me to check is the order os applied correctly. So I change LocationID to type (just to have a field with some repeating values to group by), I changed adjdate for EditDate to have different dates and I added some additional text to the result to be able to see some result:  

// var group_id = $feature["LocationID"];
var group_id = $feature["type"];
// var sql = "LocationID = '" + group_id + "'";
var sql = "type = '" + group_id + "'"; 
//var records = OrderBy(Filter($layer, sql), "adjdate ASC");
var records = OrderBy(Filter($layer, sql), "EditDate DESC");
var cnt = Count(records);

Console(cnt);
var result = "No related records"; // or fill with info when there is no data
if (cnt > 0) {
    var info = First(records);
    //result = info.feno2 + "  (" + Text(info.adjdate, "MM/DD/Y") + ")  [" + cnt + "]";
    result = info.feno2 + "  (" + Text(info.EditDate, "MM/DD/Y hh:mm:ss") + ")  [" + cnt + ":" + group_id + "]";
}

return result;

Can you confirm which layer and which fields you intent to use?

0 Kudos
AdamLevine
New Contributor

Xander,

I want the popup on the Bowers - BuildingsSQL.DBO.BWR_FireEx layer. 

I want to use LocID from the BowersTransactions layer to display the most current extinguisher, so I submitted 2 transactions for BW-228 - different dates and I would like it to display the newest FE#.   

 

My goal is: I am trying to display in the popup the current Fire Extinguisher at that location.   The process is every year all locations will have an inspection done.  Those will populate in the BowersInspections layer (I currently filtered for 2020) The ones that fail will have a transaction done to replace that extinguisher in that location which will populate the BowersTransactions layer.   So, if the FE passes the FE# will come from the most current BowersInspections layer for that location otherwise it will come from the most current BowersTransaction at that location.  Does that make sense?  That is the entire process – if I have to filter by year (like I have at this moment) that is ok. 

So hopefully to further clarify and not confuse you - looking at expr2 - this expression is close to what I need - it is pulling the BowersTransaction FE# if there is one otherwise it pulls the BowersInspections FE# however if there was an additional transaction for that location then it is not pulling the most current.   

 

Can this be accomplished with Arcade or am I pushing into JS?   I am a novice programmer – this is the first Arcade script that has been a bit beyond my abilities.  

Thanks,

Adam

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi adaml ,

Based  on what you mentioned on expr2 I think it should be changed as shown below to obtain the most recent extinguisher:

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(OrderBy(inspfe, "adjdate DESC")).extinguisherno;
    }
} else {
    // BowersTransactions found, don't check BowersInspections
    result = First(OrderBy(transfe, "adjdate DESC")).feno2;
}

return result;
0 Kudos
AdamLevine
New Contributor

xander,

Thanks so much for your time - it is still not 100% the way I want it to work but is so close that I am going to try and figure out the rest.  I don't want to take any more of your time with this.  

I really appreciate all the help you have provided.

Adam

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi adaml ,

Why don't you give it a shot and if you have any problems, I don't mind if you post any follow-up question here. My pleasure.

0 Kudos