Hi Everyone! I'm new coding and interacting with arcade, during the last weeks I've tried to implement attibute rules in order to make easier the completion of feature attributes guaranteeing data integrity.
However, once I get into this coding environment and from my poor knowledge about it, I've been trying to improve my code to make it more efficient.
Now, I've discovered that I can use just one script to calculate multiple fields, avoiding creating a single calculation rule for each field (I watched this video https://www.youtube.com/watch?v=WsO3v5kDLxg). So with that goal I started to build the code to achieve that multiple calculation in my project, but I failed misserably 🤣🤣🤣.
Not only I barely understand the sample code, I also created my own FrankenCode and even though it didn't throw any error, it doesn't work properly either.
This is the code I made:
var len = $feature.length
var wid = $feature.width
var rad = $feature.radius
if (len != 'nilReason' || wid != 'nilReason' || rad != 'nilReason') {
var len_nr = null
var wid_nr = null
var rad_nr = null
return {
//result is a dictionary or a scalar value
"result" : {
"attributes" : {
"$feature.length_nr" : len_nr, //update field1 in the $feature
"$feature.width_nr" : wid_nr, //update field2 in the $feature
"$feature.width_nr" : rad_nr
}
}
}
}
Basically I have 3 main fields (length, width and radius) and 3 secondary fields (length_nr, width_nr and radius_nr), what I want to do is when any of the 3 main fieds is different from 'nilReason', all secondary fields must be calculated as null.
I hope you can help me again...
Thanks a lot.
Eduardo.
Solved! Go to Solution.
Welcome to the amazing world of Arcade and Attribute Rules.
Your code looks good except for two small changes. Remove '$feature.' from your return. The return is looking specifficly for that field name so you returning "$feature.length_nr" is looking for a field called $feature.length_nr not length_nr. Also your return has a duplicate field. Code should look like this...
var len = $feature.length
var wid = $feature.width
var rad = $feature.radius
if (len != 'nilReason' || wid != 'nilReason' || rad != 'nilReason') {
var len_nr = null
var wid_nr = null
var rad_nr = null
return {
//result is a dictionary or a scalar value
"result" : {
"attributes" : {
"length_nr" : len_nr, //update field1 in the $feature
"width_nr" : wid_nr, //update field2 in the $feature
"radius_nr" : rad_nr
}
}
}
}
There is some other cleanup you can do but its good from there.
There are some good examples with explanations in the doc here:
To answer you question, $feature should not be in your attributes dictionary.
"result" : {
"attributes" : {
"length_nr" : len_nr, //update field1 in the $feature
"width_nr" : wid_nr, //update field2 in the $feature
"radius_nr" : rad_nr
}
}
I think I understand what you're trying to do, but you can only call return once.
var cond = [[$feature.length, "length_nr"], [$feature.width, "width_nr"], [$feature.radius, "radius_nr"]]
attributes = {}
for (var i in cond) {
if (cond[i][0] != 'nilReason') {
attributes[cond[i][1]] = null
}
}
return {'result' : {'attributes' : attributes}}
Welcome to the amazing world of Arcade and Attribute Rules.
Your code looks good except for two small changes. Remove '$feature.' from your return. The return is looking specifficly for that field name so you returning "$feature.length_nr" is looking for a field called $feature.length_nr not length_nr. Also your return has a duplicate field. Code should look like this...
var len = $feature.length
var wid = $feature.width
var rad = $feature.radius
if (len != 'nilReason' || wid != 'nilReason' || rad != 'nilReason') {
var len_nr = null
var wid_nr = null
var rad_nr = null
return {
//result is a dictionary or a scalar value
"result" : {
"attributes" : {
"length_nr" : len_nr, //update field1 in the $feature
"width_nr" : wid_nr, //update field2 in the $feature
"radius_nr" : rad_nr
}
}
}
}
There is some other cleanup you can do but its good from there.
Thanks a lot JS!
I realized that and the scripts works perfect, however I misunderstood the requirements of the feature and I need to compare each 'main' field and depending if this is different of 'nilReason' its respective 'secondary' field turns into null.
I've tried this but it seems not to be compatible with dictionary syntax:
var cond = [[$feature.length, $feature.length_nr], [$feature.width, $feature.width_nr], [$feature.radius, $feature.radius_nr]]
for (var i in cond) {
if (cond[i][0] != 'nilReason') {
return {
'result' : {
'attributes' : {
cond[i][1] : null,
}
}
}
}
}
I think line 8 is the problem, but I'm wondering if is there any way to make the fields contained in "attributes:{}" not static like 'length_nr, width_nr, radius_nr' but dynamic, to validate n data through a loop.
Any help will be highly appreciated.
Thanks again.
Eduardo
There are some good examples with explanations in the doc here:
To answer you question, $feature should not be in your attributes dictionary.
"result" : {
"attributes" : {
"length_nr" : len_nr, //update field1 in the $feature
"width_nr" : wid_nr, //update field2 in the $feature
"radius_nr" : rad_nr
}
}
Thanks a lot Ted!
I realized that and the scripts works perfect, however I misunderstood the requirements of the feature and I need to compare each 'main' field and depending if this is different of 'nilReason' its respective 'secondary' field turns into null.
I've tried this but it seems not to be compatible with dictionary syntax:
var cond = [[$feature.length, $feature.length_nr], [$feature.width, $feature.width_nr], [$feature.radius, $feature.radius_nr]]
for (var i in cond) {
if (cond[i][0] != 'nilReason') {
return {
'result' : {
'attributes' : {
cond[i][1] : null,
}
}
}
}
}
I think line 8 is the problem, but I'm wondering if is there any way to make the fields contained in "attributes:{}" not static like 'length_nr, width_nr, radius_nr' but dynamic, to validate n data through a loop.
Any help will be highly appreciated.
Thanks again.
Eduardo
I think I understand what you're trying to do, but you can only call return once.
var cond = [[$feature.length, "length_nr"], [$feature.width, "width_nr"], [$feature.radius, "radius_nr"]]
attributes = {}
for (var i in cond) {
if (cond[i][0] != 'nilReason') {
attributes[cond[i][1]] = null
}
}
return {'result' : {'attributes' : attributes}}
This is exactly what I needed, thanks a lot Ted!