I try to create a Constraint Rule to limit the values for 2 fields which are applied 2 domains as Diameter (Double) and Material (Text). Here is my valid code and I've successfully added it. However, it always returns error when I edit feature attributes and I can't seem to understand why.
var diameter = $feature.DIAMETER;
if (diameter == [24,30,36,48])
{$feature.MATERIAL == [CI, SP]}
else if (diameter == [18,20])
{$feature.MATERIAL == DI}
I try with other valid code as follow but it doesn't work out anyway.
var diameter = $feature.DIAMETER;
if (diameter == 24 || diameter == 30 || diameter == 36)
{$feature.MATERIAL == CI || $feature.MATERIAL == SP}
else if (diameter == 18 || diameter == 20)
{$feature.MATERIAL == DI}
Please help me correct if I was missing st and just so you know I'm not a developer so I'm really bad at this.
Solved! Go to Solution.
I think it should be more along the lines of thing, but there is a high dependency on the way your data is stored.
function IsValidMaterial(material, valid_materials) {
var valid = False;
for (var i = 0; i < Count(valid_materials); i++) {
if (material == valid_materials[i]) {
valid = True;
}
}
return valid;
}
var diameter = $feature.DIAMETER;
var validation = False;
if ((diameter == '24"') || (diameter == '30"') || (diameter == '36"') || (diameter == '48"')) {
var allowed_materials = ["CI", "SP"];
return IsValidMaterial($feature.MATERIAL, allowed_materials);
} else if ((diameter == '18"') || (diameter == '20"')) {
var allowed_materials = ["DI"];
return IsValidMaterial($feature.MATERIAL, allowed_materials);
} else {
return True;
}
Hi Xander, rule was added successfully but attribute data did not follow the rule sets. It seems like the rule does not affect the data at all.
Probably the condition is not met which might be caused by an error in the expression in case it does not match the data. Can you show what type of field is used for diameter and material? Not sure if this is related, but did you validate topology after he edits?
Xander Bakker As you can see from the pics above, Diameter field type is Double and Material field type is Text which are similar to 2 domains assigned for the fields.
I dont think there is relation between topology and attribute rule since topology is only used for checking spatial relationship. Moreover, both fields are in the same feature class (Main pipeline) and the data is from Esri ArcGIS Solution so it's pretty much clean and clear.
Thanks for pointing out that you already posted the domains of the fields. You are also right about the topology. There is another type of attribute rule (validation rule) that will participate in topology, but I think it hasn't been implemented yet.
Looking at the domains, there is an error in the expression regarding reading the description. See the code below:
function IsValidMaterial(material, valid_materials) {
var valid = False;
for (var i = 0; i < Count(valid_materials); i++) {
if (material == valid_materials[i]) {
valid = True;
}
}
return valid;
}
var diameter = DomainName($feature, 'DIAMETER');
var material = $feature.MATERIAL;
var validation = False;
if ((diameter == '24"') || (diameter == '30"') || (diameter == '36"') || (diameter == '48"')) {
var allowed_materials = ["CI", "SP"];
return IsValidMaterial(material, allowed_materials);
} else if ((diameter == '18"') || (diameter == '20"')) {
var allowed_materials = ["DI"];
return IsValidMaterial(material, allowed_materials);
} else {
return True;
}
... or perhaps better, just using the domain codes:
function IsValidMaterial(material, valid_materials) {
var valid = False;
for (var i = 0; i < Count(valid_materials); i++) {
if (material == valid_materials[i]) {
valid = True;
}
}
return valid;
}
var diameter = $feature.DIAMETER;
var material = $feature.MATERIAL;
var validation = False;
if ((diameter == 24) || (diameter == 30) || (diameter == 36) || (diameter == 48)) {
var allowed_materials = ["CI", "SP"];
return IsValidMaterial(material, allowed_materials);
} else if ((diameter == 18) || (diameter == 20)) {
var allowed_materials = ["DI"];
return IsValidMaterial(material, allowed_materials);
} else {
return True;
}
To validate what is happening you could use the expression in a pop-up and see what is returned and add some Console statements for debugging.
I'm sorry to say that the problem remains the same but thank you so much for your help. Please let me know if you need the full data package for more testing.
P/S: I created a simple calculation rule this morning to examine whether the data has problems or not. But it seems there is nothing wrong with the data because I used the same fields for the expression and the rule was applied successfully.
if ($feature.DIAMETER == 20) {
return 'DI'
}
else if ($feature.DIAMETER == 36) {
return 'CI'
}
Worked for me, thanks!