Application Scripts (AppScripts) are powerful and can bring a wealth of functionality to an application. This library contains common operations frequently used by the TrackVia team when engineering applications.
Working with Dates
There are many different ways to work with dates inside an app script. One of those ways is to use a calendar object. Here's an example of then app script that sets a date field to the first day of the month following the initial date entry.
//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
Working with Checkboxes
Checkboxes behavior a little differently than some of the other field types within TrackVia. They are best handled as a "List" data type. Here's an example of removing and adding a selection from a checkbox field. There are several other ways to achieve this save thing using different list operators.
//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
Throwing Exceptions
App scripts can be used to intentionally stop a user from updating, creating, or deleting a record. This is done by throwing a runtime exception, or an error message that appears when the user tries to do the action. Here's an example of displaying an error when a user tries to create a record without filling out the Sales Person field.
//String value that a form populates
String salesPerson = currentValues["Sales Person"]
//Field doesn't have a value and that's a problem
if(salesPerson == ""){
//Exception will display as a toaster when form is submitted
throw new RuntimeException("Sales Person must have a value.")
}
Using Constants
Constants allow you to reference apps, tables, and fields without hard coding their display names into the app script. Please refer to the constants article for more information. Here's a quick example of how to use a constant.
//Fields from a State table accessed from the side panel
int governor = 24930
int dateInactive = 24974
//Note how the variables take the place of hardcoded values
if(currentValues[governor] && (currentValues[governor] != previousValues[governor])){
currentValues["Previous Governor"] = currentValues[governor]
}
Creating Functions
Functions can be created to run operations within an app script. Here's an example of a function called "addOne" that takes an integer as an input, adds 1 to it, then returns that result.
def addOne(int input) {
return input + 1
}
int myNumber = 0
myNumber = addOne(myNumber)
Comments
0 comments
Please sign in to leave a comment.