Overview
This article details the syntax rules to follow while writing App Scripts in Groovy. Understanding these rules is critical to ensuring that your scripts work as intended, and operate efficiently.
The Script Editor will not allow you to save an App Script that contains incorrect syntax, even if it is something as simple as missing a closing curly bracket }
, so it is important to be mindful of these rules while writing your scripts.
1. Always reference field values using square brackets [ ]
Example: To reference a field called "Status":
//Referencing the Status field that has been declared using its constant
int status = 651;
currentValues[status]
//Referencing the Status field without using its constant
currentValues["Status"]
2. Declare Variables using def or the specific Variable type
//Declaring a Variable using def
def dueDate = calendar.getTime()
//Examples of declaring Variables as specific Variable types
String helloWorldSingle = 'Hello World' as String
int ten = 10 as int
BigDecimal num1 = 23.56 as BigDecimal
List sampleList1 = [1, 2, 3] as List
3. All Brackets {}
[]
and Parentheses ()
Must Be Matched (Open and Close)
if (value != null && value > 0) {
// logic here
}
4. Wrap All Conditions in a Single Set of Parentheses()
if (amount > 0 && status == "Open") {
// do something
}
5. Use ==
for Comparisons
Do NOT use =
when checking for equality. =
is used for assigning the value on the left side of =
to the value on the right side.
Instead, use:
if (status == "Complete") { ... }
6. Strings Must Be in Quotes
Field names and values MUST be Strings unless you've defined them as Constants using their system IDs.
currentValues["Status"] = "Complete"
7. Check for null
Before Comparing or Using Values
Avoid runtime errors by checking for null:
if (currentValues["Due Date"]) {
// safe to use it now
}
8. Use Curly Brackets {}
for Code Blocks
if (priority == "High") {
currentValues["Urgent"] = true
}
9. Avoid Reserved Words and Overwriting System Objects
Do not redefine currentValues
, previousValues
, or built-in types like Date
10. Use Commenting //
to Clarify Purpose and Logic
// Set task to Late if completed after due date
if (dueDate && dateCompleted && dueDate < dateCompleted) {
currentValues["Status"] = "Late"
}
Related Articles:
Comments
0 comments
Please sign in to leave a comment.