User avatar
Posts: 14116
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

Tips when developing Script Expressions.

Sat Sep 05, 2015 2:36 pm

Here are some general guide lines that will make your development of scripts faster and more stable.

Read this guide on Script Expressions to start with:

Write down all Use Cases you want your script to support. Write down each case step by step. Once you have developed your script, make sure you test all Use Cases.

Create a test application. Always make your development in a separate test application and not in the customer application. The test application can also be on a different server.

Use the Test Script function you have in the editor to test your script. You can set variable and trackPoint inputs in the editor. Geofences, POIs Custom Fields etc. will be taken from the selected user.

Use the log() function to log to the Terminal or the App Console.
App Console:

How to use SimOne to test Event Rules

Export the Event Rule from the test application and import it into the customer application when you are finished with development and testing.
How to:

Here is a list of script samples (we will add more over time):

Sample: Inside POI

Sample: Geofence rule that checks if a vehcile is inside a geofence named by a custom field.

Sample: Script Expression example for daily odometer interval.

Guide: Multiple events per event rule and device. Set Notifier variables from script.

Guide: Allow users to select Event Rules using Custom Fields with scripting

Guide: Notifier variables:

Guide: Trip and Idle and Parking report using event rule

Guide: Trip and Idle in Event Rule Reports

Guide: Driver Login Report

Sample: 5 km gap without position updates

Sample: Distance between POIs (customer sites)


GpsGate Support

Posts: 807
Joined: Sun Jan 09, 2011 3:32 am

RE: Tips when developing Script Expressions.

Sun Sep 06, 2015 4:15 am


Another tip when you use:
var x = session.user.getCustomFieldValue(strLabel)

make sure to have conditional is null check for var x before using it for other method's input parameter.

When Test script inside Script Editor windows, no error message will occur but when saving it inside Event Rule you might get Error in Script expression.

Best Regards,

User avatar
Posts: 19
Joined: Tue Mar 11, 2014 2:30 pm
Location: Sweden

RE: Tips when developing Script Expressions.

Wed Jan 20, 2016 1:54 pm

You can use the context.state to store more than just simple string/numeric values.

When using the state to store a more complex JavaScript object (, we recommend following these three steps.

1. In the beginning of the script, get the state object, making sure that it is not null:

var st = context.state || {};

Or, alternatively, if the structure of the state is known in advance and you need to set some initial values for the very first execution, use something like this:

var st = context.state || {
sampleVar1: 0,
sampleVar2: 'INITIAL_VALUE'

// ...

2. Now read the data from the state through the "st" object. Likewise, do any modifications to the state that are necessary directly on the "st" object.

// read
var oldValue = st.sampleVar1;
// write
st.previousSpeed = trackPoint.velocity.groundSpeed;

Note that as state is stored as a JSON object (, storing a Date ( value to the state will convert it to a string.
When retrieving the date value that was stored in the state, do this:

var properDateValue = new Date(st.someDateValue);

3. Modifications done to the "st" object will not actually be preserved until you do:


Best place to do this is after all the necessary uses and modifications, just before returning a value from the script (if the script branches out to more than one "return", do this just before each of them).


GpsGate Support

Return to “GpsGate for Developers”