Attribute rule to change one attribute value in one field based on a domain value in another field.

936
5
Jump to solution
09-30-2021 10:08 AM
RPGIS
by
Regular Contributor

Hi,

So I am trying to update one value in one field based on a selected value in another field using domains and attribute rules. So far I am struggling to figure out how to set up the attribute rule where the user selects one value from one domain which in turn automatically updates a value in another field based on matching coded values. Here is what I have so far:

var fc = $feature
var POC_fieldname = 'PointOfContact'
var Contact_fieldname = 'ContactInformation'

var PLdom_values = Domain(fc, POC_fieldname)
var PCdom_values = Domain(fc, Contact_fieldname)

var PL_codedVals = PLdom_values.codedValues
var PC_codedVals = PCdom_values.codedValues

for (var PL in PL_codedVals){
for (var PC in PC_codedVals){

if (PL == PC){
return PC
}
}
}

I haven't done anything like this in arcade, but I do it all the time in python. I am struggling a bit with this so any help on this would be greatly appreciated.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Regular Contributor
var output_domain = Domain($feature, "OutputField")// domain on the output field
var input_domain_code = $feature.InputField
var input_domain_description = DomainName($feature, "InputField", input_domain_code)

// if the codes of your domains are the same
return input_domain_code

// if the descriptions (what you see in the table) are the same
for(var od in output_domain.codedValues) {
  if(input_domain_description == od.name) {
    return od.code
  }
}
return null //no matching description found

// if you need some custom mapping (eg an output_domain code corresponds
// to multiple input_domain codes), you can use a dictionary
// {input_domain_code: output_domain_code}
var domain_code_dict = {
  1: 3,
  2: 1,
  3: 1,
  4: 2,
  5: 1
}
return domain_code_dict[input_domain_code]

If none of these fit your needs, then I need screenshots.


Have a great day!
Johannes

View solution in original post

0 Kudos
5 Replies
JohannesLindner
MVP Regular Contributor

https://developers.arcgis.com/arcade/function-reference/data_functions/#domain 

I'm not too sure what you're trying to do. Could you give us an example?

Here are a few things I noticed:

var fc = $feature // you can just call Domain($feature, ...) below.
var POC_fieldname = 'PointOfContact'
var Contact_fieldname = 'ContactInformation'

var PLdom_values = Domain(fc, POC_fieldname)
var PCdom_values = Domain(fc, Contact_fieldname)

var PL_codedVals = PLdom_values.codedValues
var PC_codedVals = PCdom_values.codedValues

// You're looping through both domains, so it will return the first
// entry of PLdom_values that corresponds to an entry in PCdom_values
// I think you want to get the PCdom_value that corresponds to the $feature's
// PLdom_value. That means only looping through PCdom_values!

// Domain.codedValues is an array of dictionaries:
// [ {"code": 1, "name": "Description"} ]

// As I said, not too sure what you want, but I guess it's more in the line of this:

var POC_description = DomainName($feature, POC_fieldname)
for(var PC in PC_codedVals) {
  if(PC.name = POC_description) {
    return PC.code
  }
}

 


Have a great day!
Johannes
0 Kudos
RPGIS
by
Regular Contributor

Hi @JohannesLindner,

Here is what I am trying to accomplish.

When the end user selects a code from one domain, loop through another domain and select the matching code, and return the value. I don't know if I explained this enough but if needed, I can provide screenshots if that may help.

0 Kudos
JohannesLindner
MVP Regular Contributor
var output_domain = Domain($feature, "OutputField")// domain on the output field
var input_domain_code = $feature.InputField
var input_domain_description = DomainName($feature, "InputField", input_domain_code)

// if the codes of your domains are the same
return input_domain_code

// if the descriptions (what you see in the table) are the same
for(var od in output_domain.codedValues) {
  if(input_domain_description == od.name) {
    return od.code
  }
}
return null //no matching description found

// if you need some custom mapping (eg an output_domain code corresponds
// to multiple input_domain codes), you can use a dictionary
// {input_domain_code: output_domain_code}
var domain_code_dict = {
  1: 3,
  2: 1,
  3: 1,
  4: 2,
  5: 1
}
return domain_code_dict[input_domain_code]

If none of these fit your needs, then I need screenshots.


Have a great day!
Johannes
0 Kudos
RPGIS
by
Regular Contributor

Thanks @JohannesLindner,

 

This is what I was looking for:

var input_domain_code = $feature.InputField
// if the codes of your domains are the same
return input_domain_code
0 Kudos
JustinKraemer
New Contributor II

I am grateful for the information in this post, but I am having trouble using the below part of the expression with the variable definitions as shown just above it. In my case the descriptions match in both fields, but one is a text field and the other an integer. The integer field is the subtype field, too, by the way.

var output_domain = Domain($feature, "Patrol")// domain on the output field
var input_domain_code = $feature.PatrolCode
var input_domain_description = DomainName($feature, "PatrolCode", input_domain_code)

 

for(var od in output_domain.codedValues) {
  if(input_domain_description == od.name) {
    return od.code
  }
}
return null //no matching description found

the trouble is that when I click the Verify button, it tells me 
Invalid expression.
Error on line 6.
Dictionary type expected

Line 6 in the error message is the one shown as line 2 above. How can I correct the error, please?

Thanks,
Justin

Tags (1)
0 Kudos