Populate a field in the current record from the previous record

621
3
Jump to solution
03-28-2022 06:19 PM
Scott_Sambell
Occasional Contributor

Hi.  Hopefully this is really simple and i'm missing something obvious.  Im using the latest fieldmaps with calculated expressions.  I have features with related records in the webmap and im creating smartforms for entering records.  I just want to grab one of the attributes from the previous record for that feature and populate a field in the new record.  The code below works but it uses the function FIRST so it grabs the first record.  I just want to grab the last, but everything i try gives an error.  This code works.  It just return the record from the wrong end of the featureset. Is there a "LAST" equivalent to "FIRST" that will work in calculated expressions?

//Recalls the most recent record for this feature and
//returns the BaitLeftAmount attribute

var recordGUID = $feature.GUID; //Grab the current GUID
var recordset = FeatureSetByName($map,'BaitRecords'); //Grab a featureset of records
var subset = filter(recordset,"Guid = @recordGUID" ); //filter down the featureset to just those with same GUID as this one
var oneline = first(orderby(subset,'DateChecked')); //takes only one line of the featureset
var baitamount = oneline.BaitLeftAmount; //takes only one attribute from the one line
return baitamount

 

@JeffShaner 

 

Scott
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I think you just need to add a "DESC" or "ASC" modifier to the order by clause to have the results sorted exactly how you want, then you can just get the first one. Like:

 

FIRST(ORDERBY(subset,'DateChecked DESC'))
FIRST(ORDERBY(subset,'DateChecked ASC'))

 

 

View solution in original post

3 Replies
Scott_Sambell
Occasional Contributor

I made this miasma that works by creating a variable that calculates the maximum ObjectID of the subset and then looks for the record with that ObjectID in the subset .  Not ideal, but it works and relies on ObjectIDs being sequential which is not always the case.  Still want to know what the opposite of FIRST is in Arcade.

 

var recordGUID = $feature.GUID;  //Grab the current GUID

var recordset = FeatureSetByName($map,'BaitRecords');  //Grab a featureset of records

var subset = filter(recordset,"GUID = @recordGUID");  //filter down the featureset to just those with same GUID as this one

var maxoid = max(subset,'OBJECTID'); //find the maximum ObjectID in the subset.  This would be a lot better if it was the maximum DateChecked but i can’t get that to work

var subsubset = filter(subset, "OBJECTID = @maxoid")  //Just grab that one line that has the max objectID

var oneline = first(orderby(subsubset,'OBJECTID')); //takes only one line of the featureset.  Not sure why this is still necessary but it is

var baitamount = oneline.BaitLeftAmount;  //takes only one attribute from the one line

return baitamount

Scott
0 Kudos
by Anonymous User
Not applicable

I think you just need to add a "DESC" or "ASC" modifier to the order by clause to have the results sorted exactly how you want, then you can just get the first one. Like:

 

FIRST(ORDERBY(subset,'DateChecked DESC'))
FIRST(ORDERBY(subset,'DateChecked ASC'))

 

 

Scott_Sambell
Occasional Contributor

Hey Aaron.  Thats EXACTLY what it needed! Thank you so much.  

Scott
Tags (1)
0 Kudos