In this blog, we use Smart Forms to explore new functionality. If you’re looking for ways to replicate the Web AppBuilder Smart Editor functionality in your new applications, check out From the Smart Editor to Smart Forms.
Over the years, we have received many enhancement requests for the Smart Editor widget, such as the ability to calculate statistics and geometries, and even to generate text values.
While we were able to implement several requests, some functionality may now be available in Smart Forms using Calculated expressions and the Arcade scripting language.
Use the Editor widget with Smart Forms to improve your editing experiences and automatically calculate attributes. This widget is available in Map Viewer and app builders such as Experience Builder and Instant Apps.
To build Forms in Map Viewer, all you need is an editable layer. If this is your first time, read this blog to get started.
Below are examples of the most popular requests, along with a sample web map to help you get started:
- Calculate statistics
- Generate text values
- Calculate values based on other field values
- Generate sequential values
- Generate random values
Calculate statistics
Use Arcade functions to calculate statistical values such as the feature’s area, length, and even the distance between two features. Perform mathematical operations such as count, sum, min, max, and mean using features and values from other fields or other layers.
Count features
Example: Count the number of customer address points intersecting with an edited polygon feature. See the sample web map.
Click here to show the sample code
Copy the code below to create your own Arcade calculated expressions:
// Define the source layer to intersect
var sourceLayer = FeatureSetByName($map, "Addresses")
// Count the number of intersecting features from the source layer
var countFeatures = Count(Intersects(sourceLayer,$feature))
// Return the value (number)
return countFeatures
Calculate an area
Example: Calculate the total area in square feet for an edited polygon feature. See the sample web map.
Click here to show the sample code
Copy the code below to create your own Arcade calculated expressions:
// Return the geodetic area for a polygon feature in the given units,
// and round to 0 decimals
Round(AreaGeodetic($feature, 'square-feet'),0)
Generate text values
Use Arcade functions to generate new text values. Fetch attribute values from features intersecting with the feature you are editing using Intersects. Then, use the Concatenate function to populate a field with these values.
Example: Get the names of all the public schools found within an edited polygon feature. See the sample web map.
Click here to show the sample code
Copy the code below to create your own Arcade calculated expressions:
// Define the source layer to fetch attribute values
var sourceLayer = FeatureSetByName($map, "Public School Locations")
// Get the features contained in the current feature's boundaries
var attributeValues = Contains($feature, sourceLayer)
// Loop through the features and get values for the field "NAME"
var valuesList = []
for (var f in attributeValues){
Push(valuesList, f.NAME)
}
// Return the list of values, separated by a comma
return Concatenate(valuesList, ', ')
Calculate values based on other field values
Use Arcade expressions to look up values from other fields within the same or another feature, and calculate new values accordingly.
Example: Look up the names of the schools within an edited polygon feature and specify with ‘Yes’ or ‘No’ if there is a high school. See the sample web map.
Click here to show the sample code
Copy the code below to create your own Arcade calculated expressions:
// Define the text field to look up
var text_field = $feature.ConcatenateValues
// Find the text value "High" within the text field
if (Find("High", text_field, 0)>-1) {
// If the string value "High" can be found, return 'Yes'
return "Yes";
// If the value "High" cannot be found, return 'No'
} else {
return "No";
}
Generate sequential values
The NextSequenceValue function is only available for database attribute rule calculations with ArcGIS Enterprise. However, you can use Arcade functions in Smart Forms to calculate the next number in a sequence based on the value for the latest created feature.
Example: Generate an identifier value by adding +1 to the identifier value from the last created feature. In this case, the value is generated when the feature is created but will not be changed when the feature is updated. See the sample web map.
Click here to show the sample code
Copy the code below to create your own Arcade calculated expressions:
// Define the edit context so the value is only calculated when the feature is created
if ($editContext.editType == "INSERT") {
// Lookup the value for the last created feature
var last_feature = First(OrderBy($layer, 'SequentialValue DESC'));
// If the last value was empty write 1, otherwise add 1 to the value
return IIf(IsEmpty(last_feature), 1, last_feature.SequentialValue + 1)
} else {
// When the feature is updated, return the existing value in this field
// and do not calculate a new value
$feature.SequentialValue
}
Use the $editcontext.editType variable to indicate whether the calculated expressions should be executed when the feature is created, updated, or deleted.
Generate random values
Values between 0 and 1 can be generated randomly for a numerical or text field when a new feature is created.
Numerical value
Example: Generate a random identifier value with 5 digits. In this case, the value is generated when the feature is created but will not be changed when the feature is updated. See the sample web map.
Click here to show the sample code
Copy the code below to create your own Arcade calculated expressions:
// New function to generate a random number that must be above the value "val"
function random_above(val) {
// Generate a random number between 0 and 1
var random_number = Random()
// If the number is below "val", generate a new random number
if (random_number <= val) {
return random_above(val)
}
// If the number is above "val". multiply by 100,000 and remove decimals
return Round(random_number *100000, 0)
}
// Define the edit context so the value is only calculated when the feature is created
// and return the result from the function "random_above" when "val"= 0.1
if ($editContext.editType == "INSERT" && $feature.RandomValue == null) {
return random_above(0.1)
} else {
// When the feature is updated, return the existing value in this field
// and do not calculate a new random value
return $feature.RandomValue
}
Text value
Example: Generate a text identifier value with a random 5-digit number and a prefix. In this case, the value is generated when the feature is created but will not be changed when the feature is updated. See sample web map.
Click here to show the sample code
Copy the code below to create your own Arcade calculated expressions:
// Define the edit context so the value is only calculated when the feature is created
if ($editContext.editType == "INSERT" && $feature.RandomValueText == null) {
// Generate a random number between 0 and 1,
// multiply it by 100,000 and preserve 5 digits
var random_text = Text(Random()*100000, '00000')
// Return the random number and add a prefix text to it
return "Prefix-" + random_text
} else {
// When the feature is updated, return the existing value in this field
// and do not calculate a new random value
$feature.RandomValueText
}
Other requests
Of course, these calculation examples barely scratch the surface of what’s possible with the Arcade scripting language. Sometimes, small things can make a big difference!
Other common requests we’ve had over the years for the Smart Editor widget which are now included in the Editor widget and the Smart Forms:
- Support for Contingent Attribute Values
- Support for Subtype Group Layers
- Better Snapping options
- Customization of the attribute form
Keep an eye out; there’s still a lot of new exciting functionality coming soon!
Article Discussion: