-
Notifications
You must be signed in to change notification settings - Fork 907
Dynamic Location Validation Approach #2200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SapphicFire
merged 15 commits into
ServiceNowDevProgram:main
from
Charanjet:Charanjeet-Hacktoberfest#2
Oct 17, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a7b7262
Create Readme.md
Charanjet fc57af8
Create Smart Attachment Size Limiter.js
Charanjet 9149ae9
Delete Server-Side Components/Business Rules/Smart Attachment Size Li…
Charanjet b60349f
Merge branch 'ServiceNowDevProgram:main' into Charanjeet-Hacktoberfest#2
Charanjet a9c13f7
Add UserLocationUtils for retrieving user location
Charanjet 9fa52c7
Add Readme for User Location Validator solution
Charanjet c15fe89
Add README for User Location Validator solution
Charanjet ce7e10f
Implement user location validation on form submission
Charanjet 8dd63ec
Refactor location validation using Haversine formula
Charanjet 6b4d248
Revise README for User Location Validator
Charanjet 62b4b52
Revise Readme for User Location Validator script
Charanjet ee37572
Implement UserLocationUtils for location retrieval
Charanjet 88e9457
Update Readme with corrected script references
Charanjet 943b7e7
Delete Server-Side Components/Script Includes/Dynamic Location Valida…
Charanjet 7f1ddbc
Delete Server-Side Components/Script Includes/Dynamic Location Valida…
Charanjet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...t-Side Components/Client Scripts/Dynamic Location Validation Approach/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| **User Location Validator** | ||
| 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. | ||
|
|
||
| **How It Works** | ||
| - The **server-side Script Include**(UserLocationUtils.js) fetches the assigned business location’s latitude, longitude, and name for the logged-in user. | ||
| - The **client-side script**(User Location Validator.js) uses the browser API to obtain the current latitude and longitude of the user at form submission. | ||
| - It calculates the distance between these two points using the **Haversine formula**, which accounts for the spherical shape of the Earth. | ||
| - The key constant `earthRadiusKm = 6371` defines the Earth's radius in kilometers and is essential for accurate distance calculation. | ||
| - 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. | ||
| - If the user is within range, a confirmation info message is displayed and the submission proceeds. | ||
|
|
||
| **Sample Output** | ||
| - **Success:** "Location validated successfully within range of Headquarters." | ||
| - **Failure:** "You are 15.23 km away from your registered location: Headquarters." | ||
|
|
||
| **Usage Notes** | ||
| - Requires user consent for geolocation access in the browser. | ||
| - The script uses descriptive variable names for clarity and maintainability. | ||
| - Suitable for scenarios requiring geo-fencing compliance or location-based workflow restrictions. |
52 changes: 52 additions & 0 deletions
52
...Components/Client Scripts/Dynamic Location Validation Approach/User Location Validator.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| function onSubmit() { | ||
| // Check if the browser supports geolocation | ||
| if ("geolocation" in navigator) { | ||
| // Request current user position | ||
| navigator.geolocation.getCurrentPosition(function(position) { | ||
| var currentLatitude = position.coords.latitude; // Current user latitude | ||
| var currentLongitude = position.coords.longitude; // Current user longitude | ||
|
|
||
| // Allowed business location coordinates fetched from server | ||
| var allowedLatitude = locData.latitude; | ||
| var allowedLongitude = locData.longitude; | ||
| var locationName = locData.name; | ||
|
|
||
| // Earth's radius in kilometers - constant used in distance calculation formula | ||
| var earthRadiusKm = 6371; | ||
|
|
||
| // Convert degree differences to radians | ||
| var deltaLatitude = (currentLatitude - allowedLatitude) * Math.PI / 180; | ||
| var deltaLongitude = (currentLongitude - allowedLongitude) * Math.PI / 180; | ||
|
|
||
| // Haversine formula components | ||
| var a = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) + | ||
| Math.cos(allowedLatitude * Math.PI / 180) * | ||
| Math.cos(currentLatitude * Math.PI / 180) * | ||
| Math.sin(deltaLongitude / 2) * Math.sin(deltaLongitude / 2); | ||
|
|
||
| var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | ||
|
|
||
| // Calculate distance in kilometers between current and allowed locations | ||
| var distanceKm = earthRadiusKm * c; | ||
|
|
||
| // Check if user's current distance exceeds tolerance (e.g., 10 km) | ||
| if (distanceKm > 10) { | ||
| alert("You are " + distanceKm.toFixed(2) + " km away from your registered location: " + locationName); | ||
| g_form.addErrorMessage("Location validation failed: Submission outside the allowed radius."); | ||
| return false; // Cancel form submission | ||
| } else { | ||
| g_form.addInfoMessage("Location validated successfully within range of " + locationName); | ||
| return true; // Allow form submission | ||
| } | ||
| }, function(error) { | ||
| alert("Geolocation error: " + error.message); | ||
| return false; // Stop submission if geolocation fails | ||
| }); | ||
|
|
||
| // Prevent form submission while waiting for async geolocation result | ||
| return false; | ||
| } else { | ||
| g_form.addErrorMessage("Geolocation is not supported by your browser."); | ||
| return false; // Block if geolocation API unsupported | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
...-Side Components/Client Scripts/Dynamic Location Validation Approach/UserLocationUtils.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| var UserLocationUtils = Class.create(); | ||
| UserLocationUtils.prototype = { | ||
| initialize: function() { | ||
|
|
||
| }, | ||
| getUserLocationCoords: function() { | ||
| var user = gs.getUser(); | ||
| var loc = user.getRecord().location; | ||
| if (loc) { | ||
| var locGR = new GlideRecord('cmn_location'); | ||
| if (locGR.get(loc)) | ||
| return { | ||
| latitude: parseFloat(locGR.latitude), | ||
| longitude: parseFloat(locGR.longitude), | ||
| name: locGR.name.toString() | ||
| }; | ||
| } | ||
| return null; | ||
| }, | ||
|
|
||
| type: 'UserLocationUtils' | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haversine! Love to see this