Skip to content

Commit 62b4b52

Browse files
authored
Revise Readme for User Location Validator script
Updated the readme to clarify the functionality and usage of the User Location Validator script, including details on how it works and sample output.
1 parent 6b4d248 commit 62b4b52

File tree

1 file changed

+18
-3
lines changed
  • Server-Side Components/Script Includes/Dynamic Location Validation Approach

1 file changed

+18
-3
lines changed
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
User Location Validator
2-
This solution ensures only users within their assigned business location can submit ServiceNow forms. The Script Include (Server-Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js) fetches location coordinates from the user’s profile. The Client Script (Client-Side Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js) compares these with the actual browser location, blocking submission if the user is outside the allowed area. Update office location in the user record to adjust the validation.
1+
**User Location Validator**
2+
This script restricts form submissions based on the physical location of the user. The current location is obtained using the browser’s geolocation API (latitude and longitude), and is then compared against the user's assigned business location stored in ServiceNow.
33

4-
If using a scoped application, ensure cross-scope access is allowed for Script Include calls.
4+
**How It Works**
5+
- The **server-side Script Include**(Server-Side Components/Script Includes/Dynamic Location Validation Approach/UserLocationUtils.js) fetches the assigned business location’s latitude, longitude, and name for the logged-in user.
6+
- The **client-side script**(Client-Side Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js) uses the browser API to obtain the current latitude and longitude of the user at form submission.
7+
- It calculates the distance between these two points using the **Haversine formula**, which accounts for the spherical shape of the Earth.
8+
- The key constant `earthRadiusKm = 6371` defines the Earth's radius in kilometers and is essential for accurate distance calculation.
9+
- If the user’s current location is outside the predefined radius (default 10 km), the form submission is blocked with an error message showing the distance and allowed location.
10+
- If the user is within range, a confirmation info message is displayed and the submission proceeds.
11+
12+
**Sample Output**
13+
- **Success:** "Location validated successfully within range of Headquarters."
14+
- **Failure:** "You are 15.23 km away from your registered location: Headquarters."
15+
16+
**Usage Notes**
17+
- Requires user consent for geolocation access in the browser.
18+
- The script uses descriptive variable names for clarity and maintainability.
19+
- Suitable for scenarios requiring geo-fencing compliance or location-based workflow restrictions.

0 commit comments

Comments
 (0)