Select to view content in your preferred language

CALCULATION RULE FOR MULTIPLE FIELDS

2514
6
Jump to solution
07-12-2023 06:30 AM
EDUARDOARDILARINCON
Occasional Contributor

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.

0 Kudos
3 Solutions

Accepted Solutions
Jake_S
by Esri Contributor
Esri Contributor

@EDUARDOARDILARINCON 

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.

 

View solution in original post

TedHoward2
Esri Contributor

There are some good examples with explanations in the doc here:

https://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/overview/attribute-rule-dictionary-k...

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
    }
}

 

View solution in original post

TedHoward2
Esri Contributor

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}}

 

View solution in original post

6 Replies
Jake_S
by Esri Contributor
Esri Contributor

@EDUARDOARDILARINCON 

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.

 
EDUARDOARDILARINCON
Occasional Contributor

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

0 Kudos
TedHoward2
Esri Contributor

There are some good examples with explanations in the doc here:

https://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/overview/attribute-rule-dictionary-k...

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
    }
}

 

EDUARDOARDILARINCON
Occasional Contributor

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

0 Kudos
TedHoward2
Esri Contributor

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}}

 

EDUARDOARDILARINCON
Occasional Contributor

This is exactly what I needed, thanks a lot Ted!

0 Kudos