Write a trigger
A WE trigger is an exported server function registered for an object in Admin Setup's WE Triggers configuration. The registration identifies its namespace, object, exported method, and active state. A trigger receives weSystem and a trigger context.
Namespace packages declare mappings in a triggers JSON file referenced by module-config.json. Each mapping identifies an object, API version, stage (before or after), operation (insert, update, delete, or undelete), and handler in moduleName.exportName form. Admin Setup displays loaded mappings and lets an administrator change their active state. Orphaned database mappings appear separately as deprecated entries.
async function validateAccount({ trigger }) {
if (trigger.isBefore && trigger.isInsert) {
for (const record of trigger.new) {
if (!record.Name) {
trigger.addError(record, 'Name is required.', { fieldName: 'Name' });
}
}
}
}
module.exports = { validateAccount };
addError(record, message, { fieldName }) marks an actual object from trigger.new as invalid. Passing a copied object or an object outside that array throws. Omit fieldName for a record-level error.
Context
| Field | Meaning |
|---|---|
new |
Array of current records; empty if no current rows are supplied. |
old |
Array of previous records, or null when unavailable. |
newMap, oldMap |
Records keyed by ID; oldMap can be null. For rows without an ID, an idx_<position> key is used. |
isBefore, isAfter |
Execution stage flags. |
isInsert, isUpdate, isDelete, isUndelete |
Record operation flags. |
Lowercase flag aliases such as isbefore also exist; use the camel case forms for readability. The context and its first-level arrays/maps are frozen by the engine; record objects inside trigger.new can be changed by a before trigger, and their mutations are passed forward. Use addError to reject a write. An after trigger can call weSystem for follow-up work, including a scheduled job or email, but account for repeat execution and avoid a write loop on the same object.
First-party product triggers
WE also loads standard product triggers from environment_modules/standard/triggers/<product>.json. These always-active definitions run in the application process before namespace triggers for the same object, stage, and operation. They are not stored in we_triggers or shown in Admin Setup's trigger list. The we-crm manifest currently covers Opportunity and Quote synchronization, Product and Pricebook Entry rules, and Opportunity and Quote line item rules. A product manifest's filename and entry order determine execution order. See core_engines/standardTriggerRunner.js and the CRM product guide.
Keep the exported function small and check the flags before touching new or old; their presence varies by operation. The existing working/environment_modules/custom/we_test/index.js contains a testTrigger example. Context construction is in working/core_engines/triggerContext.js; namespace execution is in moduleEngine.js, and trigger registration is handled in working/controllers/automationsController.js.