Hi ESRI Community,
I need a modification to a previous post I made. The previous solution works fine except for one part that I forgot to mention to the community and didn't catch when I tested it.
In this scenario I have a feature class that has a related table used for inspections. In the feature a have a field named "ConditionInspectionStatus" that has a default attribute of "No Inspection" at the time the record is created. In the inspection, I have a field named "InspectionReason" and the field staff selects either "Condition Rating" or "Maintenance Activity". When "Condition Rating" is selected the script runs fine, it updates the "No Inspection" with either "Inspection Completed" or "Inspection Attempted".
However, when "Maintenance Activity" is selected it overwrites whatever attribute is in the "ConditionInspectionStatus" field in the feature class with a null. I need it to not update the feature class field with anything when "Maintenance Activity" is selected in the inspection. I need it to leave whatever attribute that is in the field of the feature class alone.
I tried just commenting the null out however it gives me an Identifier expected for the right parenthese after the null line (line 12).
I apologize for not mentioning the default value in the feature class field and for not catching this sooner.
Any assistance in this matter would be greatly appreciated.
// abort if there is no ParnetGUID
var parent_id = $feature.ParentGUID
if (IsEmpty(parent_id)) { return parent_id }
// get the inspection status
var inspection_status= When(
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable == "Yes",
"Inspection Completed",
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable != "Yes",
"Inspection Attempted",
null // default value, for example when InspectionReason is not "Condition Rating"
)
// get the date field that should be updated
var date_field = When(
$feature.InspectionReason == "Condition Rating", "LastConditionRatingInspDate",
$feature.InspectionReason == "Maintenance Activity", "LastMaintenanceActivityDate",
null // some other inspection reason
)
// construct the update instructions
var update = {"globalID": parent_id, "attributes": {}}
update.attributes[date_field] = $feature.InspectionDate
update.attributes["ConditionInspectionStatus"] = inspection_status
if($feature.MaintenanceReason != null) {
update.attributes["MaintenanceActivityReason"] = $feature.MaintenanceReason
}
return {
'edit': [{
'className': 'GISTRANS_PLANNING_O.CTDOT_Planning_OtherDrainage',
'updates': [update]
}]
}
Your first var checks for InspectionReason being anything but "Condition Rating"....... then null. So, if InspectionReason == 'Maintenance Activity', inspection_status gets set to null.
Then, when you construct the update, you set ConditionInspectionStatus = inspection_status (ie., null).
I think adding the current value to the When statement should take care of it:
// get the inspection status
var inspection_status= When(
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable == "Yes", "Inspection Completed",
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable != "Yes", "Inspection Attempted",
$feature.InspectionReason == "Maintenance Activity", $feature.ConditionInspectionStatus, null )
So, if InspectionReason is Maint. Act., then return what is already in that field.
R_
I tried the suggested modification and although the expression validated in Pro, when I tried to do the inspection, it gave me an Arcade error: String type expected, script line 23.
In the suggested modification, it has a null at the end of it. I don't know if this is telling the script not to do anything with the "ConditionInspectionStatus" field in the feature, when "Maintenance Activity" is selected, which is what I want or if it's telling the script to make the field "CondiitionInspectionStatus" null when "Maintenance Activity" is selected, which is what I don't want.
Again, all I'm trying to do is to have the field "ConditionInspectionStatus" field in the feature not update when in the inspection, under the field "InspectionReason", "Condition Rating" is not selected. The other choice for this field is "Maintenance Activity". So when "Maintenance Activity" is selected I need to have no action take place on the "ConditionInspectionStatus" in the feature.
Below is the script:
// abort if there is no ParnetGUID
var parent_id = $feature.ParentGUID
if (IsEmpty(parent_id)) { return parent_id }
// get the inspection status
var inspection_status= When(
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable == "Yes",
"Inspection Completed",
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable != "Yes",
"Inspection Attempted",
$feature.InspectionReason == "Maintenance Activity", $feature.ConditionInspectionStatus, null )
//null // default value, for example when InspectionReason is not "Condition Rating"
// get the date field that should be updated
var date_field = When(
$feature.InspectionReason == "Condition Rating", "LastConditionRatingInspDate",
$feature.InspectionReason == "Maintenance Activity", "LastMaintenanceActivityDate",
null // some other inspection reason
)
// construct the update instructions
var update = {"globalID": parent_id, "attributes": {}}
update.attributes[date_field] = $feature.InspectionDate
update.attributes["ConditionInspectionStatus"] = inspection_status
if($feature.MaintenanceReason != null) {
update.attributes["MaintenanceActivityReason"] = $feature.MaintenanceReason
}
return {
'edit': [{
'className': 'GISTRANS_PLANNING_O.CTDOT_Planning_OtherDrainage',
'updates': [update]
}]
}
the When statement:
var inspection_status= When($feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable == "Yes", "Inspection Completed",
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable != "Yes", "Inspection Attempted",
$feature.InspectionReason == "Maintenance Activity", $feature.ConditionInspectionStatus, null )
Is telling it that if it matches the first two conditions, return "Inspection Completed", if matches both the second conditions, it returns "Inspection Attempted", if matches the third "== Maintenance Activity", return the current ConditionInspectionStatus (so, no change to the field), but, if it doesn't match any of them, return null.
Whatever value you put in that last position (where null is) is what is returned if none of the conditions are true.
However, you say that if IspectionReason == "Maintenance Activity", you want NO updates to occur.
So, if you put something like this before the When statement:
if ($feature.InspectionReason == "Maintenance Activity") { break }
It should just stop executing on this line when == "Maintenance Activity" and won't update anything.
R_
I'm not sure if I'm understanding what you're saying so I want to clarify. You stated "However, you say that if InspectionReason == "Maintenance Activity", you want NO updates to occur".
It's not that I want no updates to occur. The field "ConditionInspectionStatus" in the feature class is ONLY to be updated when in the inspection table the field "InspectionReason" has "Condition Rating" selected. If Condition Rating is not selected, the only other choice is "Maintenance Activity", so when that is selected the script is to do nothing regarding updating the field "ConditionInspectionStatus" in the feature class. However, I still need the other parts of the script to work to update the date fields.
Since I have the field "InspectionReason" in the inspection set as a required field in Field Maps and the only two choices are Condition Rating or Maintenance Activity, do I need to have the null value return or is this just best practice?
If I put your code modification before the When statement, as you suggest, do I remove the Maintenance Activity statement in the When statement? Again, when you state that this "won't update anything" are you just referring to the "ConditionInspectionStatus" field in the feature class or are you saying that this "break" will stop the script from running, which I can't have because I need the date conditions to update.
Thank you again for your assistance, I appreciate you taking time to help me.
Adding the modification before the When statement woudl break it and stop the script from running the rest of code, so you can ignore that since you have other updates as well.
So, this when statement:
var inspection_status= When(
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable == "Yes", "Inspection Completed",
$feature.InspectionReason == "Condition Rating" && $feature.OtherDrainageRatable != "Yes", "Inspection Attempted",
$feature.InspectionReason == "Maintenance Activity", $feature.ConditionInspectionStatus, 'valueifnomatch' )
will set the value of the varible inspection_staus = 'Inspection Completed" when InspectionReason (IR) = 'Condition Rating" and Other DrainageRatable (ODR) = "Yes".
If IR = 'Condition Rating" and ODR NOT equal to 'Yes", then inspection_status is set to "Inspection Attempted".
If IR = 'Maintenance Activity", the variable inspection_status is set equal to the current value in "ConditionInspectionStatus" for that feature (so, no change).
The 'valueifnomatch' (formerly null) is the value that inspection_status is set to if NONE of the When statements are true. Often this is null, but can be any valid result. Even if not used (never evaluates to false), it still needs to be there as the When statement needs the default value to be valid syntax.
Could alway set it to $feature.ConditionInspectionStatus, that way, if NONE of the When statements evalueate to True, it will return the value currenlty there, so no change.
So, the update will happen on the field regardless of the value of InspectionReason, just that if it is "Maintennace Activity", it will be "updated" the the value that is already there, so no actual change to the value.
R_