Hi everyone! I'm trying to build a constranit attribute rule on a feature, in this feature I have 6 fields (Lentgh, Lentght_Na, Width, Width_NA, Radius, Radius_NA), 3 main fields (Length, Width, Radius) referred to a domain that contains 2 options (Available, Not Available) and other 3 secondary fields (Length_NA, Width_NA, Radius_NA) that must be completed using one option af another domain (Under construction, demolished, pending) only if their respective main fields contain the option "Not Available", in case that the "Available" option is selected, the "_NA" fields must be null, so basically I need to create an alert that appears when the main field is 'Not Available' and the '_NA' field is null.
First, I've created constraint rules for each couple of fields like this:
if ($feature.length == 'Not Available' && isempty($feature.length_NA)) {
return false
}
else {
return true
}
That piece of code was useful at the beginning, but I've realized that I have so many layers, and so many other fields that uses the same structure to validate the data, so repeating this code for every single pair of fields could be time consuming.
According to the above, I've modified the script using a function an then loading each pair of fields as an input data:
function evaluate(field1,field2) {
if (field1 == 'nilReason' && isempty(field2)) {
return false
}
else {
return true
}
}
evaluate($feature.width, $feature.width_NA)
evaluate($feature.radius, $feature.radius_NA)
evaluate($feature.length, $feature.length_NA)
However, when I tested it, the alert only appears when the last case (Length) matches the conditions in the 'if' clause, the other two pair of cases are not working (the alert doesn't appear no matter what's inside the fields).
I'm barely new to coding and I have no idea where is the problem, so once again I request your help.
Thanks a lot!
Solved! Go to Solution.
This is probably the most efficient way, as you can exit after the first item you encounter that is false:
var cond = [[$feature.width, $feature.width_NA],[$feature.radius, $feature.radius_NA],[$feature.length, $feature.length_NA]]
for (var i in cond) {
if (cond[i][0] == 'nilReason' && isempty(cond[i][1])) {
return false
}
}
return true;
If you want a custom error message per field combo:
var results = [];
var cond = [[$feature.width, $feature.width_NA,"Msg A"],[$feature.radius, $feature.radius_NA,"Msg B"],[$feature.length, $feature.length_NA,"Msg C"]]
for (var i in cond) {
if (cond[i][0] == 'nilReason' && isempty(cond[i][1])) {
push(results,cond[i][2]);
}
}
if (Count(results) == 0){
return true;
}
return {'errorMessage': Concatenate(results,", ")}
This is probably the most efficient way, as you can exit after the first item you encounter that is false:
var cond = [[$feature.width, $feature.width_NA],[$feature.radius, $feature.radius_NA],[$feature.length, $feature.length_NA]]
for (var i in cond) {
if (cond[i][0] == 'nilReason' && isempty(cond[i][1])) {
return false
}
}
return true;
If you want a custom error message per field combo:
var results = [];
var cond = [[$feature.width, $feature.width_NA,"Msg A"],[$feature.radius, $feature.radius_NA,"Msg B"],[$feature.length, $feature.length_NA,"Msg C"]]
for (var i in cond) {
if (cond[i][0] == 'nilReason' && isempty(cond[i][1])) {
push(results,cond[i][2]);
}
}
if (Count(results) == 0){
return true;
}
return {'errorMessage': Concatenate(results,", ")}
Hi Mike! First of all, thanks a lot for your response, I've tried your code, I added some missing parenthesis but the expression builder shows the error "Uknown Function" I guess it doesn't recognize "evaluate" as a built-in function in Arcade.
I'll stay tuned for your response, I really need some help here.
Thanks again.
Eduardo
I edited the code, bad copy and paste error. I removed the evaluate.
Thanks a lot Mike! It works perfectly I'll dig into your code to try to understand it all.