Trigger scripts

The triggers.g form contains a field for entering JavaScript (Script). We recommend that the Script field contain single statements that invoke functions in the ScriptLibrary. For example, system.library.myTriggers.triggerXYZ( );.

Click the Compile icon in the ScriptLibrary to verify the syntax of your trigger script function. A trigger script only needs to return a value if the trigger fails. The user can indicate the failure of a trigger by returning the value of –1. Trigger scripts can access the value of the current record in memory using the global object called system.record, and they can access the value of the same record on disk using the global object called system.oldrecord. For example, system.library.myTriggers.triggerXYZ( system.record, system.oldrecord );.

Triggers example

In this example, a trigger sends a message to the assignment group identified in an incident record that the record has been updated and to whom it is assigned. It then adds an activity note that the message has been sent.

var NewRecord=system.vars.$L_new; /* This is the $L.new record passed in from the trigger */
var incNumber=NewRecord.number;
var incAssignee=NewRecord.assignee_name;
if (incAssignee==null)
{
	incAssignee="no one at the moment";
}
/* Insert a message schedule record into the schedule file */
function insertSchedule( incNumber, incAssignee, Adressees)
{
	var newSchedule = new SCFile( "schedule" );
	var today=new Date();
	newSchedule.application= "message.bg";
	newSchedule._class = "problem";
	newSchedule.name= "message processor record";
	newSchedule.expiration= today;
	newSchedule.strings[1]="1";
	newSchedule.strings[2]="problem update";
	newSchedule.strings[3]="Problem " + incNumber + " has been 
	updated. It is assigned to " + incAssignee + ".";
	newSchedule.strings[5]="pm.main";
	newSchedule.strings[6]=" Soap-Windows XP";
	newSchedule.strings1=Adressees;
	var rc = newSchedule.doInsert();
}
/* Selects the operators that are members of the assignment group to return to the calling function */
function selectAddressees(assignmentGroup)
{
	var assignment = new SCFile( "assignment" );
	var findAssignee = assignment.doSelect( " name=\""+ assignmentGroup + 
	"\"" );
	if ( findAssignee == RC_SUCCESS )
	{
		addressList=assignment.operators;
		return addressList;
	}
	else
	{
		print( "Could not find assignment group. " + 
		RCtoString( findAssignment ) );
		return null;
	}
}
/* Inserts an activity with the information that the email notification 
 to the assignment group members has been sent */
function addActivity(incNumber)
{
	var newActivity = new SCFile( "activity" );
	var today=new Date();
	newActivity.number= incNumber;
	newActivity.datestamp= today;
	newActivity.type= "Update";
	newActivity.description[0]= "Notification email on the update has been
	sent to all members of the assignment group.";
	var result = newActivity.doAction("add");
	if (result == RC_CANT_HAVE || result == 51 )
	{
		system.library.activityUpdates.scheduleActivityUpdate
		( newActivity, newActivity.type, newActivity.description );
	}
}
/* Selects the operators to send the message to */
Addressees = selectAddressees( NewRecord.assignment );
if (Addressees != null)
{
/* adds the message to the schedule file for sending */
insertSchedule( incNumber, incAssignee, Addressees);
/* adds an activity to inform the user of the email being sent*/
addActivity(incNumber);
}

The example also shows (among other things) how to call a Document Engine Action, how to call a function from the Script Library, and how to insert a new record. In addition, it shows how to use several functions within a single JavaScript and how to call them.

Note Because the class field name in the schedule file is in conflict with the class keyword used in JavaScript, it must be preceded by an underscore (_).