|
| 1 | +# 🧮 ObjectStack Formula & Expression Guide |
| 2 | + |
| 3 | +**Role:** You are the **Platform Logic Expert**. |
| 4 | +**Goal:** Write valid expressions for Formulas, Validation Rules, and Filter Logic. |
| 5 | +**Syntax:** Microsoft Excel / Salesforce-like syntax. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. Where to use Formulas? |
| 10 | + |
| 11 | +| Usage | Context (Variable) | Return Type | Example | |
| 12 | +| :--- | :--- | :--- | :--- | |
| 13 | +| **Formula Field** | Current Record | Any (Text, Number, Boolean) | `amount * 0.1` | |
| 14 | +| **Validation Rule** | Current Record | Boolean (`true` = Error) | `amount < 0` | |
| 15 | +| **Default Value** | User / Global | Value of Field Type | `NOW()` | |
| 16 | +| **Visibility** | Record + User | Boolean (`true` = Show) | `record.status == 'new'` | |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 2. Syntax Basics |
| 21 | + |
| 22 | +* **Operators:** `+`, `-`, `*`, `/`, `==`, `!=`, `>`, `<`, `>=`, `<=`, `&&` (AND), `||` (OR). |
| 23 | +* **Strings:** Use single quotes: `'Open'`. |
| 24 | +* **Merge Fields:** |
| 25 | + * Direct access: `amount`, `status` |
| 26 | + * Relationship: `account.name`, `owner.email` |
| 27 | + * Global: `$user.id`, `$today` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 3. Function Reference |
| 32 | + |
| 33 | +### 🧠 Logical Functions |
| 34 | +* **IF(condition, true_val, false_val)**: `IF(amount > 1000, 'High', 'Low')` |
| 35 | +* **ISBLANK(expression)**: Checks if value is null or empty string. `ISBLANK(end_date)` |
| 36 | +* **ISPICKVAL(field, "value")**: Checks dropdown value. `ISPICKVAL(stage, "Closed Won")` |
| 37 | +* **CASE(expression, val1, result1, val2, result2, default)**: Multiple if-else. |
| 38 | + |
| 39 | +### 🔤 Text Functions |
| 40 | +* **CONCAT(text1, text2, ...)** or `text1 + " " + text2` |
| 41 | +* **LEFT(text, num_chars)** |
| 42 | +* **LEN(text)**: Length of string. |
| 43 | +* **LOWER(text)** / **UPPER(text)** |
| 44 | +* **TEXT(value)**: Converts Number/Date to String. |
| 45 | + |
| 46 | +### 🔢 Math Functions |
| 47 | +* **ABS(number)**: Absolute value. |
| 48 | +* **ROUND(number, decimals)**: `ROUND(3.14159, 2)` -> `3.14` |
| 49 | +* **MIN(num1, num2)** / **MAX(num1, num2)** |
| 50 | + |
| 51 | +### 📅 Date Functions |
| 52 | +* **TODAY()**: Current Date. |
| 53 | +* **NOW()**: Current Date & Time. |
| 54 | +* **YEAR(date)**, **MONTH(date)**, **DAY(date)**. |
| 55 | +* **DATE(year, month, day)**. |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 4. Common Recipes |
| 60 | + |
| 61 | +### 🛑 Validation Rules (Return TRUE to block save) |
| 62 | + |
| 63 | +**Scenario 1: End Date must be after Start Date** |
| 64 | +```javascript |
| 65 | +end_date < start_date |
| 66 | +``` |
| 67 | +*Message: "End Date cannot be earlier than Start Date."* |
| 68 | + |
| 69 | +**Scenario 2: Phone is required if Contact Method is 'Phone'** |
| 70 | +```javascript |
| 71 | +ISPICKVAL(contact_method, 'Phone') && ISBLANK(phone) |
| 72 | +``` |
| 73 | +*Message: "Please provide a phone number."* |
| 74 | + |
| 75 | +**Scenario 3: Discount cannot exceed limit based on User Role** |
| 76 | +```javascript |
| 77 | +discount > 0.30 && $user.role != 'manager' |
| 78 | +``` |
| 79 | +*Message: "Only Managers can approve discounts over 30%."* |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +### 🧪 Formula Fields |
| 84 | + |
| 85 | +**Scenario 1: Full Name (Text)** |
| 86 | +```javascript |
| 87 | +first_name + " " + last_name |
| 88 | +``` |
| 89 | + |
| 90 | +**Scenario 2: Days Open (Number)** |
| 91 | +```javascript |
| 92 | +IF(ISPICKVAL(status, 'Closed'), closed_date - created_date, TODAY() - created_date) |
| 93 | +``` |
| 94 | + |
| 95 | +**Scenario 3: Priority Flag (Text/Emoji)** |
| 96 | +```javascript |
| 97 | +IF(amount > 100000, "🔥 High Value", "Standard") |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## 5. Security Expressions (RLS) |
| 103 | +Used in `sharing.yml` or `permission.zod.ts`. |
| 104 | + |
| 105 | +* **Owner Only:** `owner == $user.id` |
| 106 | +* **Team View:** `department == $user.department` |
| 107 | +* **Manager Hierachy:** `owner.manager == $user.id` |
0 commit comments