I'm working on developing a smart form for use in Field Maps, and would like a form calculation to only populate some fields when the exisiting value is empty or null. For example, when somebody creates a new feature, I want the form to populate using the below expression. This gives new records a uniqe "Observation number as per my expression.
I also want users to be able to edit features, but each time a feature is accessed in Field Maps for editing, my Observation Number is getting overwritten with a new calculation. I've tried including If Else logic to only run the calculation when the field is empty, but it doesn't seem to be working.
Anybody know what I'm missing? Thank you for your help!
//generate a "count" of milliseconds elapsed since the start of the year,
//and combine that with trapper number and survey abreviation to create a
//unique Observation Number.
//layout some variables to get milliseconds since the start of current year
var now = Now();
var currentYear = Year(now)
var yearStart = Date(currentYear, 0, 0);
//dateCheck variable used for testing only
var dateCheck = Date(2023, 0, 5);
var msCount = DateDiff(now, yearStart, 'milliseconds');
var charCount = Count(Text(msCount))
//Building out "Get Next Sequence" variables.
//Basically a number that increments every 1.67 minutes
//Different gns used depending on total character count from msCount above
//(keeps the number unique when milliseconds increases from XXX,XXX to X,XXX,XXX to XX,XXX,XXX)
var gns9 = Left(msCount, 4)
var gns10 = Left(msCount, 5)
var gns11 = Left(msCount, 6)
//Return a gns value from above based on the character count of millisecond value
var gns = When(charCount == 9, gns9, charCount == 10, gns10, charCount == 11, gns11, 'ERROR');
//Get the full name of signed in user and split into an array and grab the 4th indexed value
//For this to work, full name in user profile should include:
//First Name as a single word, and Last Name as:
//Last Name, Program Abreviation, and TrapperNumber separated by spaces
//example: John Smith JB 1111
var user = GetUser(FeatureSetByName($map, 'Survey Sites')).fullName
var trapperNum = Split(user, ' ')[3];
var yearYY = Right(Text(currentYear), 2)
//Build a unique observation number with YY + Survey Acronym + unique surveyor ID number extracted from last name + gns millisecond value
return yearYY+"EWB"+trapperNum+"X"+gns;
Here's an example of the IIF logic I tried as well, replacing line 34 above:
var observationNumber = IIf(IsEmpty($feature.observation_number), yearYY+"EWB"+trapperNum+"X"+gns, $originalFeature.observation_number)
return observationNumber
Solved! Go to Solution.
I may have just solved this myself, but still open to suggestions of any improvement since I'm not well versed in Arcade!
I replaced $originalFeature with $feature in my IIF statement and seems to like that better.
var observationNumber = IIf(IsEmpty($feature.observation_number), yearYY+"EWB"+trapperNum+"X"+gns, $feature.observation_number)
return observationNumber
I may have just solved this myself, but still open to suggestions of any improvement since I'm not well versed in Arcade!
I replaced $originalFeature with $feature in my IIF statement and seems to like that better.
var observationNumber = IIf(IsEmpty($feature.observation_number), yearYY+"EWB"+trapperNum+"X"+gns, $feature.observation_number)
return observationNumber
@DonSjoboen this is similar to what you are trying to do. might be worth a look to see if the solution would work for yours?