Validating Defined Data in AppScripts

AppScripts can be a powerful tool in extending the capabilities of a TrackVia application. A common use case is to use checkbox or dropdown fields to ensure that the values of a field are within a predefined set of values. In applications where it is absolutely crucial that values put into these fields conform to the data model definition on the field, there are a few strategies that can help validate field values.
  • Define the options in a List
  • Use native methods to prevent bugs
  • Identify worst case possibilities
  • Log information that could be useful
In cases where it is necessary for a value being set on a checkbox or dropdown field in TrackVia to be accurate, the available choices can be defined as a List of Strings. Once the list of options has been defined, Groovy can check each value it attempts to set to ensure it's a valid option. Consider the following snippet that demonstrates what this might look like on a checkbox field.
// Sample values could be from anywhere else def dynamicValue = "A"; // Possible values on a field called "Checkboxes" def possibleValues = ["A", "B", "C"]; // Check that an incoming value is a valid option if(possibleValues.contains(dynamicValue)) { currentValues["Checkboxes"] = dynamicValue; } else { // Perform some logic if its an invalid value logger.info("Invalid value: "+dynamicValue); }
If an AppScript is handling a critical piece of logic and it is important that a value be accurate, the strategy of defining values in a List can be used in conjunction with a thrown Runtime Exception to prevent a record from being created or updated with an invalid value. When a Runtime Exception is thrown, the record transaction will be rolled back and the end user will be presented with a message containing the error. Consider the following snippet in which a dropdown field attempts a validation and aborts the transaction when an invalid value is found.
// Sample values could be from anywhere else def dynamicValue = "Nonessential"; // Possible values on a field called "Drop Down List" def possibleValues = ["Important", "Urgent", "Critical"]; // Check that an incoming value is a valid option if(!possibleValues.contains(dynamicValue)) { throw new RuntimeException("Invalid value: "+dynamicValue); } else { // Allow value to be set currentValues["Drop Down List"] = dynamicValue; }