Overview
Writing an effective App Script starts with understanding how to reference data, structure logic, and keep your scripts maintainable. This guide covers the core components of scripting in TrackVia: general best practices, using Bind Variables, and an introduction to Variables and Constants.
🥇 App Script General Best Practices
- Use Commenting to provide clarifying details and to activate/deactivate code.
Typing//
at the beginning of a line of code will cause that line to be "commented out" or deactivated, and the App Script will not read it or perform any actions from that line of code.
- For single-line commenting, use//
- For multi-line or interline commenting, use/**/
- You can also highlight a section of multiple lines of code and press CNTRL+ / to comment those lines in or out simultaneously.
Example: Commenting In App Scripts
//Only run this logic if the Priority field's value has changed
if (currentValues["Priority"] != previousValues["Priority"]) {
// Set the updated date
currentValues["Last Priority Change"] = new Date()
}
In this example, the 1st and 3rd lines where the script's author has provided clarifying details regarding the script are commented out using // so that the script will "skip over" those lines.
- Above each section of script, define the script's objective, include the date the section of script was written, and the script's author. For example:
//Objective: Calculate and stamp in date values for "End Date From Today"
//Date: 9/23/2021
//Author: Matt Kruzicki
-
Variables should be named in camelCase: the first word is in lowercase, and all following words begin with an uppercase letter (e.g. dueDate) which helps to provide clarity while reading the script.
- Use Constants to prevent scripts from throwing errors if an App name, Table name, or Field name is changed. (see Constants section in this article for more information).
🧠 Understanding Variables and Constants
What are Variables?
Variables are used to temporarily store data during script execution. They allow you to:
- Reuse values without repeating expressions
- Make your code easier to read and maintain.
- Reduce the risk of errors when updating your code.
In App Scripts, there are different Variable Types that can be used to store different types of data. The type of data that you want the Variable to work with determines which Variable Type you need to use. The available Variable Types are:
Variable Type | Data Type Used For |
String |
Text |
Date |
Dates |
List |
An array |
Map |
A list of key an value pairs |
Int / Integer |
A whole number |
Double |
64 bit floating decimal (e.g. 12.34567) |
Float |
32 bit floating decimal (e.g. 12.34) |
BigDecimal |
Big numbers with big decimals - preferred for performing math in scripts |
Boolean |
True/False |
def |
Used to declare a Variable |
Using Bind Variables to Reference Data in Fields
In TrackVia, there are two "Bind Variables" that are used in App Scripts to access record data:
Bind Variable | What it is |
currentValues |
A Map of all the fields and values from the record that triggered the script to run. |
previousValues |
A Map of all the fields and previous values from the record that triggered the script to run. |
Example: Using Bind Variables in an App Script
//Purpose: Set the "Date Inactive" field to the updated date of the record
currentValues["Date Inactive"] = currentValues["Updated"]
♾️ What are Constants?
App scripts reference the names of Apps, Tables, Fields, and Relationships to perform their actions. However, if the name of a referenced App, Table, Field, or Relationship has been changed; the references to it in the script will no longer match, causing the script to fail.
Each App, Table, Field, and Relationship in TrackVia has an underlying unique numeric identifier (ID) that never changes regardless of whether the name of that App/Field/Table/Relationship is edited.
In App Scripts you can define a Variable and set it equal to the unique identifier, and that is referred to as a Constant.
By using Constants in scripts, if a App, Field, Table, or Relationship name is edited in the future, it will not impact the script since the underlying ID remains the same.
Related Articles:
Comments
0 comments
Please sign in to leave a comment.