Select to view content in your preferred language

IF Else Statements in Arcade Expression

25551
16
Jump to solution
08-28-2018 01:21 AM
VyNguyen3
Occasional Contributor

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.

16 Replies
XanderBakker
Esri Esteemed Contributor

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;
}
VyNguyen3
Occasional Contributor

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. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

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?

0 Kudos
VyNguyen3
Occasional Contributor

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.

0 Kudos
XanderBakker
Esri Esteemed Contributor

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.

VyNguyen3
Occasional Contributor

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'
}

Tiffany_Chau
Occasional Contributor

Worked for me, thanks!

0 Kudos