Select to view content in your preferred language

IF statement for a constraint

1190
5
Jump to solution
08-18-2021 12:09 PM
V1212
by
Emerging Contributor

Im trying to create a constraint where if street_status=1 then the street_id must be null. 

I tried this statement but I get an error message each time I try to update street_status or street_id

if ($feature.street_status==1 && $feature.street_id==null) 

return true;

else 

return false; 

 

Thanks in advance!

 

 

 

 

 

 

0 Kudos
2 Solutions

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Probably you missed the curly brackets when copying the code, but still:

if ($feature.street_status==1 && $feature.street_id==null) {
  return true;
}
return false; 

The problem with this code is the following: If you are inserting a street with street_status 2, it will return false, because you only check for street_status 1 and return false otherwise. This should work in those cases:

if ($feature.street_status==1 && $feature.street_id!=null) {
  return false;
}
return true;

This defaults to returning true and only blocks the insert/edit on the combination you don't want.

 

Other things to check:

  • As Chris said: Is street_status numeric?
  • Instead of the null check, you could try IsEmpty($feature.street_id)

Have a great day!
Johannes

View solution in original post

HusseinNasser2
Esri Contributor

The constraint rule does the following: Only allow features to be edited/created when their status is 1 and the street_id is null. Any other value, fail the edit which is what you are experiencing. 

 

You might want to define your logic more explicitly. I'm assuming you want to ONLY fail when the status is 1 AND the street_Id is not NULL. 

 

You can specify your logic this way.  

 

 

if ($feature.street_status==1 && $feature.street_id != null) 

return false;

else 

return true; 

 

 

 

 

constr.gif

 

 

 

View solution in original post

5 Replies
ChrisGAEG
Regular Contributor

Hi there, 

What error are you getting? Is street_status a numeric field? 

V1212
by
Emerging Contributor

Im getting the constraint popup when I change the street_status to any value. 

 

Thanks!

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Probably you missed the curly brackets when copying the code, but still:

if ($feature.street_status==1 && $feature.street_id==null) {
  return true;
}
return false; 

The problem with this code is the following: If you are inserting a street with street_status 2, it will return false, because you only check for street_status 1 and return false otherwise. This should work in those cases:

if ($feature.street_status==1 && $feature.street_id!=null) {
  return false;
}
return true;

This defaults to returning true and only blocks the insert/edit on the combination you don't want.

 

Other things to check:

  • As Chris said: Is street_status numeric?
  • Instead of the null check, you could try IsEmpty($feature.street_id)

Have a great day!
Johannes
V1212
by
Emerging Contributor

Thanks for the help. 

 

I didn't realized that street_status wasn't numeric.

0 Kudos
HusseinNasser2
Esri Contributor

The constraint rule does the following: Only allow features to be edited/created when their status is 1 and the street_id is null. Any other value, fail the edit which is what you are experiencing. 

 

You might want to define your logic more explicitly. I'm assuming you want to ONLY fail when the status is 1 AND the street_Id is not NULL. 

 

You can specify your logic this way.  

 

 

if ($feature.street_status==1 && $feature.street_id != null) 

return false;

else 

return true; 

 

 

 

 

constr.gif