|
| 1 | +/** |
| 2 | + * Formula Field Examples |
| 3 | + * |
| 4 | + * Demonstrates how to use formula fields (calculated, rollup, auto-number) |
| 5 | + */ |
| 6 | + |
| 7 | +import type { FormulaField } from '@objectos/plugin-automation'; |
| 8 | + |
| 9 | +// Example 1: Calculated field - Full name |
| 10 | +export const fullNameFormula: FormulaField = { |
| 11 | + name: 'fullName', |
| 12 | + objectName: 'contacts', |
| 13 | + type: 'calculated', |
| 14 | + config: { |
| 15 | + type: 'calculated', |
| 16 | + expression: '{firstName} + " " + {lastName}', |
| 17 | + returnType: 'string', |
| 18 | + }, |
| 19 | + createdAt: new Date(), |
| 20 | +}; |
| 21 | + |
| 22 | +// Example 2: Calculated field - Age from birthdate |
| 23 | +export const ageFormula: FormulaField = { |
| 24 | + name: 'age', |
| 25 | + objectName: 'contacts', |
| 26 | + type: 'calculated', |
| 27 | + config: { |
| 28 | + type: 'calculated', |
| 29 | + expression: 'Math.floor((Date.now() - new Date({birthDate}).getTime()) / (365.25 * 24 * 60 * 60 * 1000))', |
| 30 | + returnType: 'number', |
| 31 | + }, |
| 32 | + createdAt: new Date(), |
| 33 | +}; |
| 34 | + |
| 35 | +// Example 3: Calculated field - Discount percentage |
| 36 | +export const discountPercentageFormula: FormulaField = { |
| 37 | + name: 'discountPercentage', |
| 38 | + objectName: 'opportunities', |
| 39 | + type: 'calculated', |
| 40 | + config: { |
| 41 | + type: 'calculated', |
| 42 | + expression: '({listPrice} - {finalPrice}) / {listPrice} * 100', |
| 43 | + returnType: 'number', |
| 44 | + }, |
| 45 | + createdAt: new Date(), |
| 46 | +}; |
| 47 | + |
| 48 | +// Example 4: Calculated field - Days until expiration |
| 49 | +export const daysUntilExpirationFormula: FormulaField = { |
| 50 | + name: 'daysUntilExpiration', |
| 51 | + objectName: 'contracts', |
| 52 | + type: 'calculated', |
| 53 | + config: { |
| 54 | + type: 'calculated', |
| 55 | + expression: 'Math.ceil((new Date({endDate}).getTime() - Date.now()) / (24 * 60 * 60 * 1000))', |
| 56 | + returnType: 'number', |
| 57 | + }, |
| 58 | + createdAt: new Date(), |
| 59 | +}; |
| 60 | + |
| 61 | +// Example 5: Rollup SUM - Total opportunity amount |
| 62 | +export const totalOpportunityAmountFormula: FormulaField = { |
| 63 | + name: 'totalOpportunityAmount', |
| 64 | + objectName: 'accounts', |
| 65 | + type: 'rollup', |
| 66 | + config: { |
| 67 | + type: 'rollup', |
| 68 | + relatedObject: 'opportunities', |
| 69 | + relationshipField: 'accountId', |
| 70 | + aggregateField: 'amount', |
| 71 | + operation: 'SUM', |
| 72 | + }, |
| 73 | + createdAt: new Date(), |
| 74 | +}; |
| 75 | + |
| 76 | +// Example 6: Rollup COUNT - Number of contacts |
| 77 | +export const numberOfContactsFormula: FormulaField = { |
| 78 | + name: 'numberOfContacts', |
| 79 | + objectName: 'accounts', |
| 80 | + type: 'rollup', |
| 81 | + config: { |
| 82 | + type: 'rollup', |
| 83 | + relatedObject: 'contacts', |
| 84 | + relationshipField: 'accountId', |
| 85 | + aggregateField: 'id', |
| 86 | + operation: 'COUNT', |
| 87 | + }, |
| 88 | + createdAt: new Date(), |
| 89 | +}; |
| 90 | + |
| 91 | +// Example 7: Rollup COUNT with conditions - Number of open cases |
| 92 | +export const numberOfOpenCasesFormula: FormulaField = { |
| 93 | + name: 'numberOfOpenCases', |
| 94 | + objectName: 'accounts', |
| 95 | + type: 'rollup', |
| 96 | + config: { |
| 97 | + type: 'rollup', |
| 98 | + relatedObject: 'cases', |
| 99 | + relationshipField: 'accountId', |
| 100 | + aggregateField: 'id', |
| 101 | + operation: 'COUNT', |
| 102 | + conditions: [ |
| 103 | + { |
| 104 | + field: 'status', |
| 105 | + operator: 'equals', |
| 106 | + value: 'open', |
| 107 | + }, |
| 108 | + ], |
| 109 | + }, |
| 110 | + createdAt: new Date(), |
| 111 | +}; |
| 112 | + |
| 113 | +// Example 8: Rollup AVG - Average deal size |
| 114 | +export const averageDealSizeFormula: FormulaField = { |
| 115 | + name: 'averageDealSize', |
| 116 | + objectName: 'users', |
| 117 | + type: 'rollup', |
| 118 | + config: { |
| 119 | + type: 'rollup', |
| 120 | + relatedObject: 'opportunities', |
| 121 | + relationshipField: 'ownerId', |
| 122 | + aggregateField: 'amount', |
| 123 | + operation: 'AVG', |
| 124 | + conditions: [ |
| 125 | + { |
| 126 | + field: 'stage', |
| 127 | + operator: 'equals', |
| 128 | + value: 'closed_won', |
| 129 | + }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + createdAt: new Date(), |
| 133 | +}; |
| 134 | + |
| 135 | +// Example 9: Rollup MIN - Earliest task due date |
| 136 | +export const earliestTaskDueDateFormula: FormulaField = { |
| 137 | + name: 'earliestTaskDueDate', |
| 138 | + objectName: 'projects', |
| 139 | + type: 'rollup', |
| 140 | + config: { |
| 141 | + type: 'rollup', |
| 142 | + relatedObject: 'tasks', |
| 143 | + relationshipField: 'projectId', |
| 144 | + aggregateField: 'dueDate', |
| 145 | + operation: 'MIN', |
| 146 | + conditions: [ |
| 147 | + { |
| 148 | + field: 'status', |
| 149 | + operator: 'not_equals', |
| 150 | + value: 'completed', |
| 151 | + }, |
| 152 | + ], |
| 153 | + }, |
| 154 | + createdAt: new Date(), |
| 155 | +}; |
| 156 | + |
| 157 | +// Example 10: Rollup MAX - Latest activity date |
| 158 | +export const latestActivityDateFormula: FormulaField = { |
| 159 | + name: 'latestActivityDate', |
| 160 | + objectName: 'accounts', |
| 161 | + type: 'rollup', |
| 162 | + config: { |
| 163 | + type: 'rollup', |
| 164 | + relatedObject: 'activities', |
| 165 | + relationshipField: 'accountId', |
| 166 | + aggregateField: 'activityDate', |
| 167 | + operation: 'MAX', |
| 168 | + }, |
| 169 | + createdAt: new Date(), |
| 170 | +}; |
| 171 | + |
| 172 | +// Example 11: Auto-number - Invoice number |
| 173 | +export const invoiceNumberFormula: FormulaField = { |
| 174 | + name: 'invoiceNumber', |
| 175 | + objectName: 'invoices', |
| 176 | + type: 'autonumber', |
| 177 | + config: { |
| 178 | + type: 'autonumber', |
| 179 | + prefix: 'INV-', |
| 180 | + startingNumber: 10001, |
| 181 | + digits: 6, |
| 182 | + }, |
| 183 | + createdAt: new Date(), |
| 184 | +}; |
| 185 | + |
| 186 | +// Example 12: Auto-number - Purchase order number |
| 187 | +export const purchaseOrderNumberFormula: FormulaField = { |
| 188 | + name: 'poNumber', |
| 189 | + objectName: 'purchase_orders', |
| 190 | + type: 'autonumber', |
| 191 | + config: { |
| 192 | + type: 'autonumber', |
| 193 | + prefix: 'PO-', |
| 194 | + suffix: '-US', |
| 195 | + startingNumber: 1, |
| 196 | + digits: 5, |
| 197 | + }, |
| 198 | + createdAt: new Date(), |
| 199 | +}; |
| 200 | + |
| 201 | +// Example 13: Auto-number - Support ticket number |
| 202 | +export const ticketNumberFormula: FormulaField = { |
| 203 | + name: 'ticketNumber', |
| 204 | + objectName: 'support_tickets', |
| 205 | + type: 'autonumber', |
| 206 | + config: { |
| 207 | + type: 'autonumber', |
| 208 | + prefix: 'TICKET-', |
| 209 | + startingNumber: 100000, |
| 210 | + digits: 7, |
| 211 | + }, |
| 212 | + createdAt: new Date(), |
| 213 | +}; |
| 214 | + |
| 215 | +// Example 14: Auto-number - Employee ID |
| 216 | +export const employeeIdFormula: FormulaField = { |
| 217 | + name: 'employeeId', |
| 218 | + objectName: 'employees', |
| 219 | + type: 'autonumber', |
| 220 | + config: { |
| 221 | + type: 'autonumber', |
| 222 | + prefix: 'EMP', |
| 223 | + startingNumber: 1001, |
| 224 | + digits: 4, |
| 225 | + }, |
| 226 | + createdAt: new Date(), |
| 227 | +}; |
| 228 | + |
| 229 | +// Example 15: Calculated field - Status badge |
| 230 | +export const statusBadgeFormula: FormulaField = { |
| 231 | + name: 'statusBadge', |
| 232 | + objectName: 'leads', |
| 233 | + type: 'calculated', |
| 234 | + config: { |
| 235 | + type: 'calculated', |
| 236 | + expression: '{status} === "hot" ? "🔥 Hot Lead" : {status} === "warm" ? "☀️ Warm Lead" : "❄️ Cold Lead"', |
| 237 | + returnType: 'string', |
| 238 | + }, |
| 239 | + createdAt: new Date(), |
| 240 | +}; |
| 241 | + |
| 242 | +// Example 16: Rollup SUM with multiple conditions - Revenue this quarter |
| 243 | +export const revenueThisQuarterFormula: FormulaField = { |
| 244 | + name: 'revenueThisQuarter', |
| 245 | + objectName: 'accounts', |
| 246 | + type: 'rollup', |
| 247 | + config: { |
| 248 | + type: 'rollup', |
| 249 | + relatedObject: 'opportunities', |
| 250 | + relationshipField: 'accountId', |
| 251 | + aggregateField: 'amount', |
| 252 | + operation: 'SUM', |
| 253 | + conditions: [ |
| 254 | + { |
| 255 | + field: 'stage', |
| 256 | + operator: 'equals', |
| 257 | + value: 'closed_won', |
| 258 | + }, |
| 259 | + { |
| 260 | + field: 'closeDate', |
| 261 | + operator: 'greater_than', |
| 262 | + value: '{{quarterStartDate}}', |
| 263 | + }, |
| 264 | + ], |
| 265 | + }, |
| 266 | + createdAt: new Date(), |
| 267 | +}; |
0 commit comments