Controllers
Elixir hooks that run around a record's insert, save, submit, cancel and delete.
A controller is a module named <App>.Controllers.<RecType without spaces>. For CRM Task in the CRM app it is Crm.Controllers.CRMTask. Every installed app may have one for the same RecType, and all of them run, in app order. None replaces another.
A validate hook
CRM's Holiday List checks its dates and sets a count:
defmodule Crm.Controllers.CRMHolidayList do
@behaviour Cardinal.Controller
@impl true
def validate(rec, _old, _ctx) do
from = rec["from_date"]
to = rec["to_date"]
rows = rec["holidays"] || []
cond do
from > to ->
invalid("to_date", "To Date cannot be before From Date")
row = Enum.find(rows, &(&1["date"] < from or &1["date"] > to)) ->
invalid("holidays", "The holiday on #{row["date"]} is not between From Date and To Date")
true ->
{:ok, Map.put(rec, "total_holidays", length(rows))}
end
end
defp invalid(path, message),
do: {:error, {:invalid, [%{path: path, code: "invalid", message: message}]}}
endEvery hook gets the record, the stored record (old, nil on insert) and the ctx. Records are maps with string keys. Dates and decimals are strings, Int values are integers, and a Table is a list of row maps.
Return values
| Return | Effect |
|---|---|
:ok | The record goes on unchanged |
{:ok, rec} | The next hook and the write see rec |
{:ok, rec, effects} | Plus functions that run after the commit, never on rollback |
{:error, reason} | Stops the lifecycle and rolls back |
An effect is a zero-arity function, or {:warning, text}, which the desk shows as a toast. Send mail, broadcast and enqueue in effects, since a hook never commits.
Hook order
insert: before_insert → naming → before_validate → validate → before_save → write → after_insert → on_update → on_change
save: before_validate → validate → before_save → write → on_update → on_change
submit: before_validate → validate → before_submit → write → on_update → on_submit → on_change
cancel: before_cancel → write → on_cancel → linked-record check → on_change
delete: on_trash → on_change → back-link check → delete → after_deleteA save of a submitted record runs before_update_after_submit and on_update_after_submit. A discard runs before_discard and on_discard.
A record a hook returns after the write is not stored. Write later changes with Cardinal.DB.set_value/5. A hook cannot change the docstatus.
Hooks with effects
CRM Task assigns its user after insert. Cardinal.Assign.add returns effects, and the hook passes them on (shortened):
@impl true
def after_insert(rec, _old, ctx), do: assign(rec, ctx)
defp assign(rec, ctx) do
case rec["assigned_to"] do
blank when blank in [nil, ""] ->
{:ok, rec, []}
user ->
with {:ok, _users, fx} <-
Cardinal.Assign.add(ctx, "CRM Task", rec["name"], [user], description: rec["title"]),
do: {:ok, rec, fx}
end
endOther callbacks
| Callback | Use |
|---|---|
autoname(rec, ctx) | Names a new record. Return {:ok, rec} with name set |
before_rename(rec, new, merge, ctx), after_rename(rec, old, merge, ctx) | Join a rename |
ignore_linked_rectypes(rec, ctx) | RecTypes whose submitted records do not block this cancel |
on_recurring(new, reference, auto_repeat, ctx) | Adjusts the copy an Auto Repeat makes |
Hooks on another app's RecType
A controller module named after the RecType works from any app. The manifest's rec_events is the other way in: it attaches one module to several RecTypes, or to all of them. Its functions take the RecType's name first and run after the RecType's controllers.
rec_events: [{"Purchase Invoice", GstCompliance.Controllers.PurchaseInvoiceEvents}]def validate(_rectype, rec, _old, ctx) do
rec = GstCompliance.BillOfEntry.set_pending_boe_qty(rec)
{:ok, InwardSupplyService.match_status(Cardinal.DB.as_system(ctx), rec)}
end"*" as the RecType runs the module for every RecType. {rectype, module, when_installed: :erp} runs only on sites that have ERP.