Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User Location Validator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add more comments to your read me file with an output ? This will help to understand How the current user location is fetched& get evaluated with defined range.

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.

If using a scoped application, ensure cross-scope access is allowed for Script Include calls.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function onSubmit() {
var ga = new GlideAjax('UserLocationUtils');
ga.addParam('sysparm_name', 'getUserLocationCoords');
ga.getXMLAnswer(function(response) {
var locData = JSON.parse(response);
if (!locData) {
g_form.addErrorMessage("No assigned location found for your profile.");
return false;
}

navigator.geolocation.getCurrentPosition(function(position) {
var userLat = position.coords.latitude;
var userLng = position.coords.longitude;
var allowedLat = locData.latitude;
var allowedLng = locData.longitude;
var locName = locData.name;

var R = 6371;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Charanjet
Could you please explain and add comment here line number 18 ?

var dLat = (userLat - allowedLat) * Math.PI / 180;
var dLng = (userLng - allowedLng) * Math.PI / 180;
var a = Math.sin(dLat / 2) ** 2 +
Math.cos(allowedLat * Math.PI / 180) *
Math.cos(userLat * Math.PI / 180) *
Math.sin(dLng / 2) ** 2;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var distance = R * c;

if (distance > 10) { // 10 km tolerance
alert("You are " + distance.toFixed(2) + " km away from your registered office: " + locName);
g_form.addErrorMessage("Location validation failed.");
return false;
} else {
g_form.addInfoMessage("Location validated successfully within range of " + locName);
return true;
}
});
});
return false; // Wait for async location validation
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User Location Validator
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.

If using a scoped application, ensure cross-scope access is allowed for Script Include calls.
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'
};
Loading