Select to view content in your preferred language

Arcade Expression for Barcode Scanning Verification

100
2
Jump to solution
Tuesday
RangerofGIS
New Contributor

Hello,

I'm trying to get the following arcade expression to work. Essentially the app is designed to scan bins, however sometimes the wrong bin gets scanned so I'm trying to implement a validation check so that only certain bin prefixes are allowed to be scanned and submitting but I'm getting the following message when I use this expression

boolean: false

the console also throws out

Failed: Empty or non-text

Result: false

// barcode variable from feature attribute
var barcodeValue = 'BTR7981006';
Console('Barcode Value: ' + barcodeValue);

// Define valid prefixes for Bin
var validPrefixes = [
'BTR75', 'BTR76', 'BTR780', 'BTR781', 'BTR7820', 'BTR7821', 'BTR7831',
'BTR7846', 'BTR7847', 'BTR7848', 'BTR7855', 'BTR7866', 'BTR789', 'BTR790',
'BTR792', 'BTR798'
];

var isValid = false;

// Check if barcode is empty or not a text value
if (IsEmpty(barcodeValue) || TypeOf(barcodeValue) != 'Text') {
Console('Failed: Empty or non-text');
isValid = false;
}
// Check if barcode starts with BTR and is 10 characters long
else if (Left(barcodeValue, 3) != 'BTR' || Length(barcodeValue) != 10) {
Console('Failed: Invalid format, BTR=' + Left(barcodeValue, 3) + ', Length=' + Text(Length(barcodeValue)));
isValid = false;
}
// Check if barcode starts with any valid prefix
else {
for (var prefixIndex in validPrefixes) {
var currentPrefix = validPrefixes[prefixIndex];
if (Left(barcodeValue, Length(currentPrefix)) == currentPrefix) {
Console('Passed: Matched prefix ' + currentPrefix);
isValid = true;
break;
}
}
}

// Return validation result
Console('Result: ' + Text(isValid));
return isValid;

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

There are a few things that need to be fixed in your code.

  • The TypeOf documentation seems to be incorrect. It returns "String" instead of "Text" for text. (line 15)
  • Use "Count" instead of "Length" to return the length of a string (lines 20, 21, and 28)
// barcode variable from feature attribute
var barcodeValue = 'BTR7981006';
Console('Barcode Value: ' + barcodeValue);

// Define valid prefixes for Bin
var validPrefixes = [
'BTR75', 'BTR76', 'BTR780', 'BTR781', 'BTR7820', 'BTR7821', 'BTR7831',
'BTR7846', 'BTR7847', 'BTR7848', 'BTR7855', 'BTR7866', 'BTR789', 'BTR790',
'BTR792', 'BTR798'
];

var isValid = false;

// Check if barcode is empty or not a text value
if (IsEmpty(barcodeValue) || TypeOf(barcodeValue) != 'String') {
  Console('Failed: Empty or non-text');
  isValid = false;
}
// Check if barcode starts with BTR and is 10 characters long
else if (Left(barcodeValue, 3) != 'BTR' || Count(barcodeValue) != 10) {
  Console('Failed: Invalid format, BTR=' + Left(barcodeValue, 3) + ', Length=' + Text(Count(barcodeValue)));
  isValid = false;
}
// Check if barcode starts with any valid prefix
else {
  for (var prefixIndex in validPrefixes) {
    var currentPrefix = validPrefixes[prefixIndex];
    if (Left(barcodeValue, Count(currentPrefix)) == currentPrefix) {
      Console('Passed: Matched prefix ' + currentPrefix);
      isValid = true;
      break;
    }
  }
}

// Return validation result
Console('Result: ' + Text(isValid));
return isValid;

 

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

There are a few things that need to be fixed in your code.

  • The TypeOf documentation seems to be incorrect. It returns "String" instead of "Text" for text. (line 15)
  • Use "Count" instead of "Length" to return the length of a string (lines 20, 21, and 28)
// barcode variable from feature attribute
var barcodeValue = 'BTR7981006';
Console('Barcode Value: ' + barcodeValue);

// Define valid prefixes for Bin
var validPrefixes = [
'BTR75', 'BTR76', 'BTR780', 'BTR781', 'BTR7820', 'BTR7821', 'BTR7831',
'BTR7846', 'BTR7847', 'BTR7848', 'BTR7855', 'BTR7866', 'BTR789', 'BTR790',
'BTR792', 'BTR798'
];

var isValid = false;

// Check if barcode is empty or not a text value
if (IsEmpty(barcodeValue) || TypeOf(barcodeValue) != 'String') {
  Console('Failed: Empty or non-text');
  isValid = false;
}
// Check if barcode starts with BTR and is 10 characters long
else if (Left(barcodeValue, 3) != 'BTR' || Count(barcodeValue) != 10) {
  Console('Failed: Invalid format, BTR=' + Left(barcodeValue, 3) + ', Length=' + Text(Count(barcodeValue)));
  isValid = false;
}
// Check if barcode starts with any valid prefix
else {
  for (var prefixIndex in validPrefixes) {
    var currentPrefix = validPrefixes[prefixIndex];
    if (Left(barcodeValue, Count(currentPrefix)) == currentPrefix) {
      Console('Passed: Matched prefix ' + currentPrefix);
      isValid = true;
      break;
    }
  }
}

// Return validation result
Console('Result: ' + Text(isValid));
return isValid;

 

RangerofGIS
New Contributor

Thank you so much! I was pulling my hair out wondering what was wrong.

I understand why you changed the script to string instead of text as text isnt truly an acceptable field type that ESRI uses.

However how did you also know length was not the right function to use?

0 Kudos