Field Calculation rules for date entry

350
3
Jump to solution
05-24-2023 12:34 PM
CodyYager
New Contributor

Hi,

I am trying to create some attribute rules to work with our Cartegraph instance so we can keep track of data that has been retired by our Public Works crews. I have created two simple expressions to calculate a date field and to alter a lifecycle field, but they will not allow me to manually populate the date field and retire a feature. 

 

Here are the two expressions, the first calculates the date for a feature thats lifecycle is not asbuilt or unknown. The second is supposed to calculate the lifecycle as abandoned if there is a date in the CarteRetire field. I assume the majority of the problem lies in the second expression; I am not very good at scripting yet. Both of these are set to trigger on updates only.

if ($feature.LIFECYCLE == "ASBUILT,UNKNOWN" || $feature.LIFECYCLE == $originalfeature.LIFECYCLE){
return NULL
} else {
return Date()
}

 

if (IsEmpty($feature.CarteRetire))
return $feature.LIFECYCLE
else {
return "ABANDONED"
}

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

The behavior you want isn't that simple...

Your two rules influence each other. The first rule looks at the field that gets changed by the second rule and vice versa. It might be better to do it in one rule:

 

// Calculation Attribute RUle
// field: empty!
// triggers: update

var lifecycle = $feature.TextField1
var old_lifecycle = $originalfeature.TextField1
var retire_date = $feature.DateField1
var old_retire_date = $originalfeature.DateField1


// Was the date changed to a non-null value?
var abandon_feature = retire_date != null && retire_date != old_retire_date

// Was the lifecycle changed to "ABANDONED"?
var feature_was_abandoned = lifecycle == "ABANDONED" && lifecycle != old_lifecycle

// Calculate the new values
var new_retire_date = When(
    abandon_feature, retire_date,      // the date was changed by the user, use that value
    feature_was_abandoned, Date(),     // the lifecycle was changed to "ABANDONED", use current date
    lifecycle != "ABANDONED", null,    // the feature is not abandoned, set the date to null
    retire_date                        // the feature is abandoned, some other field was updated, use existing value
)
var new_lifecycle = IIf(
    abandon_feature, "ABANDONED",    // the date was changed by the user, abandon the feature
    lifecycle                        // some other field was updated, use the existing value
)

return {
    result: {attributes: {
        DateField1: new_retire_date,
        TextField1: new_lifecycle
    }}
}

 

Change the field names in lines 5-8 and 31-32.


Have a great day!
Johannes

View solution in original post

0 Kudos
3 Replies
MikeMillerGIS
Esri Frequent Contributor

You are missing brackets on the first if statement

if (IsEmpty($feature.CarteRetire)){
  return $feature.LIFECYCLE
}
else {
  return "ABANDONED"
}
0 Kudos
JohannesLindner
MVP Frequent Contributor

The behavior you want isn't that simple...

Your two rules influence each other. The first rule looks at the field that gets changed by the second rule and vice versa. It might be better to do it in one rule:

 

// Calculation Attribute RUle
// field: empty!
// triggers: update

var lifecycle = $feature.TextField1
var old_lifecycle = $originalfeature.TextField1
var retire_date = $feature.DateField1
var old_retire_date = $originalfeature.DateField1


// Was the date changed to a non-null value?
var abandon_feature = retire_date != null && retire_date != old_retire_date

// Was the lifecycle changed to "ABANDONED"?
var feature_was_abandoned = lifecycle == "ABANDONED" && lifecycle != old_lifecycle

// Calculate the new values
var new_retire_date = When(
    abandon_feature, retire_date,      // the date was changed by the user, use that value
    feature_was_abandoned, Date(),     // the lifecycle was changed to "ABANDONED", use current date
    lifecycle != "ABANDONED", null,    // the feature is not abandoned, set the date to null
    retire_date                        // the feature is abandoned, some other field was updated, use existing value
)
var new_lifecycle = IIf(
    abandon_feature, "ABANDONED",    // the date was changed by the user, abandon the feature
    lifecycle                        // some other field was updated, use the existing value
)

return {
    result: {attributes: {
        DateField1: new_retire_date,
        TextField1: new_lifecycle
    }}
}

 

Change the field names in lines 5-8 and 31-32.


Have a great day!
Johannes
0 Kudos
CodyYager
New Contributor

Thank you! I had a feeling it was going to be a bit more complex than what I had originally planned. 

0 Kudos