How to use custom fields with event rules

Using this method you will allow your application users to select their notification preferences for their vehicles' event rules. This is especially useful in a restricted environment with users with limited roles. You as an administrator will save time with fewer user modification requests.

The users can do it by themselves in a very intuitive way by just modifying the vehicle properties and select the values in the GUI. In our example, we will create a combo box (Custom Field) that is going to be available in the Vehicle Properties (User Access Mask) where the user can select and save which notification type he wants for a speeding alert for that vehicle. The speed limit value is also a property that the user can set for each vehicle and will define the execution of the event rule.

Steps

  1. Add the Custom Fields
  2. Modify the User Type
  3. Create a new User Access Mask
  4. Create a new role and assign the Access Mask
  5. Create your Event Rules with the script
  6. Testing

Add the Custom Fields

Read how to enable user custom fields here.

Screenshot 2024-07-29 at 11.48.53.png

We have to select the Custom Field list and put here all the different notifications that our Event Rules will handle. We have to remember what we have typed in the Item and Value columns because we will call them later in our scripts.

Screenshot 2024-07-29 at 11.51.04.png

We also want to add another Custom Field called Speed Limit of the type Text

Screenshot 2024-07-29 at 11.53.14.png

Modify the User Type

Now that we have these Custom Fields set we need to add them to our Vehicles’ properties. To simplify this task we will modify the Device User Type in the User Type Editor.

Screenshot 2024-07-29 at 11.53.39.png

We select User Type Device, and then we will add our new Custom Fields created previously, under Details

Screenshot 2024-07-29 at 11.54.16.png

Screenshot 2024-07-29 at 11.55.38.png

If we edit a Vehicle we can see now the two added Custom Fields

Screenshot 2024-07-29 at 11.56.17 1.png

Create your Event Rules with the script

Now we have to create Event Rules for each different notification we want to send. As we created three different choices for our notifications (Email, SMS, Marker, Disabled) we will use three different Event Rules. One is for email, another for SMS, and another for marker.

Screenshot 2024-07-29 at 14.08.20.png

Email notification case

When creating your Event Rule, on step 4. Expressions, select Script Expression.

Read how to enable script expressions here.

In this example, we will use a simple Javascript code that will compare the Speed Limit value from the vehicle properties and return true if matches the condition.

A script for the Email notification could be like this:

var notifType = session.user.getCustomFieldValue ('Speeding Alert'); //from the Vehicle properties
var limit = session.user.getCustomFieldValue ('Speed Limit'); //from the Vehicle properties
var speed = fields.get('Speed', null)*3.6; //current Vehicle speed with mt/s to Km/h conversion
 
if(notifType == 'Email' && speed > limit) return true;
 
// else
 
return false;

Remember that the values you are using in the function getCustomFieldValue and the comparison values inside the IF clause should match the values and custom fields labels set up previously.

mceclip7.png

Select Email as the notification type for this rule

mceclip8.png

When you've configured your email notification rule, Save it.

SMS notification case

Create a new rule for SMS cases.

The SMS and Marker scripts change slightly. In the following images, we highlight the changes you need to make:

blog_evCfScr_image_016.png

Marker case

For the Marker notification case, we create a new event rule, and introduce the changes shown below.

mceclip9.png

We won't select any notification on this step (because we just want a marker on the map, we do that on step 6), we click on Next.

mceclip10.png

We select our on-map options as below

mceclip11.png

Save now the rule.

Testing

Verify for your vehicle/s which custom fields need to be selected, according to the notification type you with for each vehicle.

1. Edit the vehicle.

2. Input the values for Speeding Alert and Speed Limit.

These values will define which notification (event rule) will be used and at which speeds these alerts will react.

Screenshot 2024-07-29 at 14.22.42.png

We recommend using the device simulator to test the behavior of these new rules and notifications.