Click Script: Auto-switch between business and off-hours trips

Sometimes fleet vehicles will not only be used in business but can also be driven on private trips. For privacy reasons, private trips should not be tracked or recorded in the system. For example, the vehicle will run for business from 8:00 AM to 5:00 PM and outside that period, all trips should be considered private ones. 

With this solution, we are changing the "Position Source" option for the vehicle(s) to disable/enable the vehicle's position on the system. That allows privacy customization. 

Manually setting trip privacy with the user type editor

To disable the position source of a vehicle, edit the User type (using the User Type Editor) of the type Device and enable Position source to be visible.

mceclip0.png

Then, edit the user, and manually enable/disable the Position source property according to your needs. Selecting Ignore position will make the vehicle not to be traceable anymore.

Screenshot_2021-04-14_at_14.24.11.png

With larger fleets, this can be time-consuming work. It's hard to switch vehicles at the end of the day during a small window of time. The operator would need to edit them one by one manually. Let's learn how to automate it!

Automating business & off-hours trip privacy

You can use a click script to automate this task. The timer click script works to switch the Position source shown before, on or off. 

1. Go to Admin > Developer > App Builder

2. Create an App called “Switch Business and Private” and assign it _Administrator role.

Screenshot_2021-04-14_at_13.49.32.png

3. Add two Click Scripts named "SwitchToBusiness" and "SwitchToPrivate."

4. Choose the Timer kind, and specify the time of the first run. If hours/minutes/seconds fields are left at 0, this script will run once per day at the specified time.

Screenshot_2021-04-14_at_13.49.38.png

5. Enter the following Script for the SwitchToBusiness Click Script:

// Script for Timer actions.

// Get User Tag by Name
var tag = tags.getTagByName('Show on map');

// Get users under the Tag
var users = tag.userIDs;

for(var i in users)
{
var u = directory.getUserByID(users[i]);
// Set true to disable the position and false to enable the position.
u.setHidePosition(false);

log("Switch " + u.username +" to Business")
}
log("OK");

6. Enter the following script for SwitchToPrivate:

// Script for Timer actions.

// Get User Tag by Name
var tag = tags.getTagByName('Show on map');

// Get users under the Tag
var users = tag.userIDs;

for(var i in users)
{
var u = directory.getUserByID(users[i]);
// Set true to disable the position and false to enable the position.
u.setHidePosition(true);

log("Switch " + u.username +" to Private")
}
log("OK");

7. Test and Save both Click Scripts.

 

Then the script will run at the time which you specified to switch the business and private trip, using or ignoring the device positions according to your selection.

In the script console (Admin > Development > App Console), you also will be able to see the log of the script in action: 

Screenshot_2021-04-14_at_14.32.28.png