PHP in triggers¶
A trigger is PHP that runs inside the case. This page explains what that code receives, how it reads and writes case data, and how to find out why it failed.
Where the code runs¶
The trigger runs on the server, in the context of the case that fired it. Before it runs, the code is parsed: a syntax error stops execution and reports the trigger name and the line, so a broken trigger never runs halfway.
Output is captured, not printed: echo and print do not reach the browser. The way
to leave evidence is to write to a variable, a case note or a log.
Reading a case variable¶
Inside the code, case variables are written with the same prefix as in a condition, and the engine replaces them with the stored value:
| Prefix | Reads as |
|---|---|
@@name |
Text |
@%name |
Whole number |
@#name |
Decimal number |
@=name |
The original value, unchanged |
if (@@Priority == "high") {
@@DueDate = date("Y-m-d", strtotime("+2 days"));
}
A variable the case has not stored yet reads as an empty value. That is convenient and
dangerous: a typo in @@Prioroty does not raise an error, it just evaluates false.
Writing a case variable¶
Assign to the same prefixed name:
@@Approved = "yes";
@%Attempts = @%Attempts + 1;
The value is stored in the case when the trigger ends, and it appears in the change log like a value typed in a form.
Grids and lists¶
A grid is an array of rows. Reach one cell with the row and the column:
$total = 0;
foreach (@@Items as $row) {
$total += (float) $row["Price"];
}
@@Total = $total;
The same value can be read directly: @@Items[0]["Price"].
What the session tells you¶
The trigger runs in the case session, so these keys are available:
| Key | Value |
|---|---|
$_SESSION['APPLICATION'] |
The case id |
$_SESSION['INDEX'] |
The delegation index of the current task |
$_SESSION['TASK'] |
The task id |
$_SESSION['PROCESS'] |
The process id |
$_SESSION['USER_LOGGED'] |
The user who triggered it |
Prefer the functions that take an explicit id, such as PMFNewCase() or
PMFSendMessage(), when the trigger can run from more than one place.
Calling functions¶
The Lurana functions are available by name: cases, tasks, documents, email, users, dates and queries. They are ordinary PHP functions, so they compose:
$user = PMFGetNextAssignedUser($_SESSION["TASK"]);
if (!empty($user["USR_EMAIL"])) {
PMFSendMessage($_SESSION["APPLICATION"], "no-reply@example.com", $user["USR_EMAIL"],
"", "", "Case moved", "notification.html");
}
When it fails¶
- Syntax error — the trigger does not run at all; the message names the trigger and the line.
- Runtime error — the error is caught and stored on the case; the step that fired the trigger may not complete.
- Silent logic error — no message at all: a misspelled variable, a comparison between text and number, a condition that never matches.
The case debugging view shows the trigger that ran, its code, and the variables before and after, which is where to look first.
Practice¶
- Keep a trigger short; move reusable logic to a function or to another trigger.
- Do not hardcode task or process ids: read them from the case or look them up by name.
- Guard against empty values before using them in a calculation.
- Do not call
exitordie: they end the request, not just the trigger. - A trigger that sends email or creates a case is not idempotent — running it twice sends twice.
Related¶
- Triggers — creating and attaching them.
- Lurana functions — the function reference.
- Conditions — the same prefix rules in expressions.
- Debugging a case — the view that shows what ran.