Using Custom Triggers you can add new fields to a transaction, or set the value for a transaction field. You can use any language feature you need to compute the value of a transaction field, or securely add one.
There are two methods of the tNG object that allow adding and setting the value of a transaction fields:
addColumn - allows adding a column to the transaction
setColumnValue - allows setting the value for a specific column
Triggers that modify a transaction columns must be of type BEFORE, in order for the changes to be considered when executing the transaction.
The basic structure of such a trigger is as follows:
to add a new transaction field:
value = myvalue //code that computes the actual column value
tNG->addColumn(column_name, type, value, "")
to set the value of a transaction field:
value = myvalue //code that computes the actual column value
tNG->setColumnValue(column_name, value)
The examples below use the Discussion Board tutorial database structure, and enhances the message posting page.
The first example assumes that the subject field has been left out of the insert transaction, and it is automatically set to the first 30 characters of the message. The Custom Trigger is of type BEFORE, and the code is as follows:
For PHP:
$newSubject = substr($tNG->getColumnValue("content_msg"),0,30);
$tNG->addColumn("subject_msg","STRING_TYPE","VALUE",$newSubject);
For ASP_VBScript:
newsubject = left(tNG.getColumnValue("content_msg"),30)
tNG.addColumn "subject_msg", "STRING_TYPE", "VALUE",
newsubject, ""
For ColdFusion:
subjectValue = left(tNG.getColumnValue("content_msg"),30);
tNG.addColumn("subject_value","STRING_TYPE","VALUE",subjectValue,"");
In the following example, the subject field is displayed on the page as a text field, but it is not mandatory to be filled. A BEFORE custom trigger checks if the user entered anything, and if not, it sets the column value to the first 30 letters of the content field:
For PHP:
if($tNG->getColumnValue("subject_msg")
== "") {
$newSubject = substr($tNG->getColumnValue("content_msg"),0,30);
$tNG->setColumnValue("subject_msg",$newSubject);
}
For ASP_VBScript:
If(tNG.getColumnValue("subject_msg") = "") Then
newsubject = left(tNG.getColumnValue("content_msg"),30)
tNG.addColumn "subject_msg", "STRING_TYPE", "VALUE",
newsubject, ""
End If
For ColdFusion:
if(tNG.getColumnValue("subject_msg") EQ "") {
subjectValue = left(tNG.getColumnValue("content_msg"),30);
tNG.addColumn("subject_msg","STRING_TYPE","VALUE",subjectValue,"");
}
In the following topic you will learn how to create a custom trigger that performs a check for a unique value on more than one field.