Hi all!
I have a web map created to use in Field Maps for data collection and inspections of stormwater features. I used related tables with the parent "Inlets" and child "Inspections". I needed to copy the fields from the most recent child record to the parent record. Using the calculated expression below I was able to do so. I thought it was working great until I went to test in Field Maps and encountered "Failed to calculate expression" for all of the fields with the calculated value. It will not let me create a new parent feature because of this in mobile (the same alert appears in web but will let the feature be created and from there works fine). I've tried making those fields hidden but then get the error "5 hidden attributes failed" when submitting. At a loss for how to get this to work. Help please.
Solved! Go to Solution.
There are a couple things to consider here now that you have this working for the most part.
/* Checking Edit Context First, Return Null If INSERT or DELETE. */
if (Lower($editContext.editType) != 'update') { return null };
... The rest of your code goes here and only executes if the above condition is not met.
Note this version will NOT allow overwrites of existing values (it will only appear to calculate once). I chose one of the fields in your form, it would change for each field.
/* Checking Edit Context First, Return Null If INSERT or DELETE. */
if (Lower($editContext.editType) != 'update') { return null };
/* Checking if the current field already has a value. If so, return that value. */
if (!IsEmpty($feature.last_inspection_date)) { return $feature.last_inspection_date};
... The rest of your code goes here and only executes if the above conditions are not met.
var GID = $feature.GlobalID;
var sortedRecords = OrderBy(Filter(FeatureSetByName($datastore, "Inlet_Inspections"), 'GUID = @GID'), "Date");
... The rest of your code here.
There have been reports that the FeatureSetByRelationshipName function is buggy in Field Maps. The suggested workaround is to use FeatureSetByName and Filter the records using the key to get the related records.
Hello,
So when you create a new parent record we would expect no children to exist and you are seeking to return nulls in that case. This might be a syntax issue in your code. The mobile runtime for Field Maps is much more opinionated than that of the JavaScript API in my experience. Map viewer will let you get away with a lot more than the mobile app and this lends itself to code that works when authoring and testing expressions that then don't work in Field Maps.
The following doesn't look like valid syntax (missing curly brackets), but the JavaScript API seems to know what you mean and either handles the bad syntax or was always hitting the else and therefore just skipping line 3.
...
if (count == 0)
return null
else {
var inlet = first(relatedrecords)
return inlet["Overall_Condition"]
}
Meanwhile, the mobile app doesn't like it as it is more opinionated. You may want to try with the code below. Note: Because your 'if' statement has an explicit 'return' in it, you don't need the explicit 'else'. The 'else' can be implicit here, as shown below. I find the implicit 'else' to be much easier to read and use them whenever I can.
...
if (count == 0) { return null };
var inlet = First(relatedrecords);
return inlet['Overall_Condition'];
I hope this is all that it is. If not, you may get some insight by looking at the troubleshooting logs in Field Maps, as it may give you the expression name and line number that caused the calculation error.
Thanks Justin, I combined your advice and Ken's- we're getting somewhere! Now I have solved the issue of the error in Field Maps (error still shows in web actually) but the fields don't calculate after I complete an inspection. It will only calculate if I go back into the parent feature to edit. Thoughts?
var GID = $feature.GlobalID;
var relrec = FeatureSetByName($datastore, "Inlet_Inspections");
var result = Filter(relrec, 'GUID = @GID');
var sortedRecords = OrderBy(result,"Date");
var cnt = Count(sortedrecords);
if (cnt == 0) {return null};
var inlet = First(sortedrecords);
return inlet['Overall_Condition']
There are a couple things to consider here now that you have this working for the most part.
/* Checking Edit Context First, Return Null If INSERT or DELETE. */
if (Lower($editContext.editType) != 'update') { return null };
... The rest of your code goes here and only executes if the above condition is not met.
Note this version will NOT allow overwrites of existing values (it will only appear to calculate once). I chose one of the fields in your form, it would change for each field.
/* Checking Edit Context First, Return Null If INSERT or DELETE. */
if (Lower($editContext.editType) != 'update') { return null };
/* Checking if the current field already has a value. If so, return that value. */
if (!IsEmpty($feature.last_inspection_date)) { return $feature.last_inspection_date};
... The rest of your code goes here and only executes if the above conditions are not met.
var GID = $feature.GlobalID;
var sortedRecords = OrderBy(Filter(FeatureSetByName($datastore, "Inlet_Inspections"), 'GUID = @GID'), "Date");
... The rest of your code here.