Select to view content in your preferred language

How to calculate domain value not the domain code in code block using python

1811
2
Jump to solution
04-28-2023 07:26 AM
Labels (1)
PLadd
by
Frequent Contributor

I'm calculating field1 using code block and python.  The script checks if field2 value is not None.  If true, I need it to calculate field1 to be the domain value not the domain code of field2.  I'll provide the script below.  Checking "Enforce Domains" does not help.  Result is always the domain code not the domain value.  Result should be 'N' (domain value) and not 3 (domain code).

PLadd_2-1682691896846.png

 

PLadd_1-1682691774982.png

 

Thanks for any guidance.

 

0 Kudos
1 Solution

Accepted Solutions
CarsonG
Frequent Contributor

The value will always be the code from the domain in your script not the description. Unless you're only wanting to display the descriptions in the table.

CarsonG_1-1682700063182.png

Otherwise, if you want N to be there you would have to add N as a code to the domain for this value to be acceptable.

Currently N is the description of the code 3 within your domain.

 

View solution in original post

2 Replies
CarsonG
Frequent Contributor

The value will always be the code from the domain in your script not the description. Unless you're only wanting to display the descriptions in the table.

CarsonG_1-1682700063182.png

Otherwise, if you want N to be there you would have to add N as a code to the domain for this value to be acceptable.

Currently N is the description of the code 3 within your domain.

 

PLadd
by
Frequent Contributor

Thanks @CarsonG .  You saved me a little time and diverted my attention to figuring if this will work in Arcade, which I'm happy to report, does allow you to capture domain values.  For anyone trying to figure this out with Python (or stuck figuring it out with Arcade), below is my Calculate Field code that will test if certain values are null or not and calculate the domain values into another field accordingly.

PLadd_1-1682709144708.png

HERE'S JUST THE CODE:

//this calculates the field with house, prefix, street, type, and suffix but
//checks if prefix and/or suffix are null - if they are, don't include them

//define variables of the fields to be used
var myHouse = $feature.house_no
//to get the domain value of certain fields (not the domain code), use DomainName function
var myPrefix = DomainName($feature, 'STprefixID')
var myStreet = DomainName($feature, 'STnameID')
var myType = DomainName($feature, 'STtypeID')
var mySuffix = DomainName($feature, 'STsuffixID')

//if Prefix and Suffix for are BOTH null, then just use House, street and type
if (IsEmpty(myPrefix) && IsEmpty(mySuffix)) {
return Concatenate(myHouse, ' ', myStreet , ' ', myType);
} else {
//never do Prefix AND Suffix both have values in any one reocrd, so this test will work
if (IsEmpty(mySuffix)) {
return Concatenate(myHouse, ' ', myPrefix, ' ', myStreet , ' ', myType);
} else {
return Concatenate(myHouse, ' ', myStreet , ' ', myType, ' ', mySuffix);
}
}