Overview
This guide explains what Constants are, why they should be used, and best practices to follow when using them. It also covers how to find the underlying System IDs for Apps, Tables, Fields, and Relationships, and highlights the difference between Relationship fields and Relationship IDs and what they are used for in App Scripts.
♾️ Why Use Constants?
It is important to understand that App Scripts exist outside the context of the App's Table architecture - this means that when something is edited on a Table such as the name of a field, the App Script does not automatically update any references to that field name in the Script.
You can reference a field as a string of static text in a script by wrapping the field name in quotes (e.g. "Status Field"), but the script will only work if it can find an exact match for that name. If the field name is changed from "Status Field" to "Order Status", the static text name in the script no longer matches, which will cause the script to throw a runtime error.
However, by using the Status Field's System ID (which remains constant regardless of whether the field's name changes), the script will always be able to find the field's matching System ID and won't break when the name is changed.
🔎 Where to Find System IDs
To find the underlying unique identifiers in the App Script editor:
- Click the arrow on the right side of the screen to open the "System IDs" slide-out drawer
- All Applications are listed in alphabetical order with the app you are currently working in being automatically expanded showing all Tables in the app
- Expand any Table to see its list of Fields and Relationships. The underlying unique ID is the number in parentheses listed next to each Field/Relationship in the list.
- Click the clipboard beside an ID to copy the constant declaration, then paste it into your script editor. Now you can reference the name of Constant in your script!
🧠 Know the Difference Between Relationship fields and Relationships
It is important to be aware of the distinction between Relationship fields and Relationship IDs and what they are used for in App Scripts.
In App Scripts, Relationship fields have two parts (with separate System IDs) that can be referenced:
1. The Relationship field where the Values are stored. This is found under the "Fields" section of the System IDs.
2. The Relationship link from the current Table to the Parent Table. This is found under the "Relationships" section of the System IDs. This is ONLY used in conjunction with the addChild()
or getChildren()
functions when you need to specify the relationship in the function.
In the example above there two System IDs under the Cities Table drop down list that are each named "State". Although they have the same name, they have different System ID numbers indicating that they are not identical:
1. The Relationship field with the ID 657 (see red box in screenshot shown above) - Use this when working with the values stored in the Relationship field.
2. The Relationship (the link between the Cities Table and the State Parent Table) with the ID 249 (see blue box in screenshot shown above). Use this ONLY when utilizing the addChild()
or getChildren()
functions in your script.
🥇Best Practices for Using Constants
- Use Constants instead of "hardcoding" field names and Relationships as Variables in the script to avoid breaking the script if the field name is ever changes in the future.
- Use clear, descriptive names written in camelCase for your Constants.
- Use Commenting
//
to organize Constants by their Table, and to clarify the field type. - Since both the Relationship field and the Relationship have the same name, be sure to clearly differentiate which Constant is for the each one by appending differentiators to the end of each Constant name (e.g.
stateRelField
as the name for the State Relationship field, andstateLink
as the name for the State Relationship).
- When used in Functions, Constants should be enclosed in parentheses
()
instead of being wrapped in quotes""
like static field names.
Related Articles:
Comments
0 comments
Please sign in to leave a comment.