Select to view content in your preferred language

Form Calculation with $originalFeature return error "Unexpected Null Value"

827
3
09-29-2022 07:00 PM
JustinReynolds
Frequent Contributor

I'm attempting to us $originalFeature as a means to bail early on my more costly form field calculations.  The error feels like a bug rather than a limitation, but perhaps I'm missing something.

Everything executes fine in the FM Arcade Editor, but FM throws the error "Unexpected Null Value" on points out the line that contains a reference to $originalFeature (line 4). This is present on both iOS & Android.

 

//expr_service_date
console('Calculating Service Date based on Service Number...');

var oServiceNumber = $originalFeature["service_number"];
var cServiceNumber = $feature["service_number"];
var self = $feature["service_date"];

//Test Values
// var oServiceNumber = "'02081600.1658 (20220811)";
// var cServiceNumber = "'02081600.1659 (20220812)";

// Exit Early - return self
if (oServiceNumber == cServiceNumber) {
    // if the service number didn't change return current value
    console('>>> No change detected, returning self');
    console('   >>> ' + self);
    return self;
};

// Do the costly things here only if a change is detected

 


@Anonymous User Any thought?

Thanks

- Justin Reynolds, PE
0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

Try this:

var self = $feature["service_date"];
if($originalFeature == null) {
    return self
}
var oServiceNumber = $originalFeature["service_number"];
var cServiceNumber = $feature["service_number"];

Have a great day!
Johannes
0 Kudos
JustinReynolds
Frequent Contributor

Good idea. 

So the error is gone (at least it never makes to the line providing the error), but no matter the change to the form it always returns self... which is a null service date.

So $originalFeature == Null continues to evaluate to True.

- Justin Reynolds, PE
0 Kudos
JustinReynolds
Frequent Contributor

Okay so things work better when the edit type is UPDATE.  After some testing it appears that the state $originalFeature is not dynamic within your form (state doesn't keep up with your changes), but rather it is literally the $originalFeature.  On UPDATE, $originalFeature is not null and has a state (the state as last submitted), On INSERT, $originalFeature has no state or simply it is always null.  So you would want to combine the edit context edit type with your $originalFeature to prevent the endless circle or the error.

Unfortunately, this doesn't provide the utility I was hoping for.  We would need something that preserves the state of each field (prior to a change) dynamically WHILE editing the form rather than just the state as last submitted.

Given

$OriginalFeature -> The state as last submitted (not dynamic within an edit session)

$Feature -> The current state (dynamic within an edit session)

we would need something like

$formerFeature or $exFeature or $previousFeature -> The previous state (dynamic with an edit session)

 

- Justin Reynolds, PE