TrackVia app scripts have access to most Groovy data types. When writing app scripts, you should aim to keep the code as simple and concise as possible, which means using strict data types whenever possible. This will help to reduce the possibility of errors and make the code easier to debug and maintain.
The most common Groovy data types that you will use in app scripts are strings, booleans, numbers, and lists. These data types are usually sufficient for most tasks, though you may also need to use maps or other more complex data types for more complex tasks. Many data types can be cast from one type to another, which can be helpful when dealing with more complex data types.
It is important to remember that app scripts are run inside of a sandbox and have limited access to the underlying operating system and other external systems. This means that any data types or functions that require access to external resources should be avoided in order to ensure the security of the system.
Primitives
boolean isTruthyValue = true;
boolean isFalseValue = false;
char yes = 'Y';
int number = 10;
int negativeNumber = -120;
float decimal = 1.01;
float fraction = 0.75;
Strings
String fullName = "John Smith";
Dates
// Create date variable
Date theDate = currentValues["Date Field"] as Date
// Create calendar variable for date manipulation
Calendar calendar = Calendar.getInstance()
// Set calendar date to date variable
calendar(theDate)
// Add one month to date
calendar.add(Calendar.MONTH, 1)
// Set calendar date to the first day of the month
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
// Set date variable equal to the calendar variable
theDate = calendar.getTime()
// Set date field to the new date
currentValues["Date Field"] = theDate
Checkboxes
// Use a list for the checkbox field
List checkbox = currentValues["Checkbox Field"] as List
// Check South as a checkbox option
checkbox.add("South")
// Uncheck East as a checkbox option
checkbox.remove("East")
// Set the new value of the checkbox field
currentValues["Checkbox Field"] = checkbox
Lists
List emptyList = [];
List numberList = [1, 2, 3, 4, 5];
List stringList = ["thingA", "thingB", "thingC"];
List mixedList = [123, "John Smith", 1.75, 'Y'];
List listOfMaps = [
[key: "value"],
[key: "value"],
[key: "value"]
];
Collections
Map emptyMap = [:];
Map person = [name: "John Smith", age: 50, address: "123 Main Street"];
Map mapOfMaps = [
keys: [
[key: "value"],
[key: "value"]
],
items: [
[item: "content"],
[item: "content"]
]
];
Date today = new Date();
Date fiveDaysFromNow = new Date() + 5;
Date yesterday = new Date() - 1;
Bind Variables
Special variables that are always available in AppScripts.
Used for accessing data in the underlying tables.
currentValues["Field"];
A Map containing the values of the record that triggered the AppScript.
These values are accessed using a field or relationship name.
currentValues["Field"] = "newValue";
previousValues["Field"];
A Map containing the values of the record that triggered the AppScript.
These values are accessed using a field or relationship name.
previousValues is only populated in before update and after update AppScripts.
String oldValue = previousValues["Field"];
childTables["Child Table"]["Relationship Name"];
A List of Maps that will add child records to a parent.
When the AppScript finishes, these Maps are converted to child records.
List children = childTables["Child Table"]["Relationship Name"] as List<Map>;
children.add(["Field": "Value"]);
childRecords["Child Table"]["Relationship Name"];
A List of Maps which represents a collection of child records.
List<Map> children = childRecords["Child Table"]["Relationship Name"] as List<Map>;
children.each {
child -> logger.info(child.field)
}
Comments
0 comments
Please sign in to leave a comment.