Skip to content

Commit 10f9f7a

Browse files
Auto-populate location variables for catalog item
1 parent fa55ac7 commit 10f9f7a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ========================================
2+
// CATALOG CLIENT SCRIPT
3+
// ========================================
4+
// Name: Auto-populate Location Variables
5+
// Type: onLoad
6+
// UI Type: Desktop (or All)
7+
// Applies to: [Your Catalog Item]
8+
// ========================================
9+
10+
function onLoad() {
11+
// Get current logged-in user's ID
12+
var userId = g_user.userID;
13+
14+
// Call Script Include to get user's location information
15+
var ga = new GlideAjax('LocationUtilsAjax');
16+
ga.addParam('sysparm_name', 'getUserLocation');
17+
ga.addParam('sysparm_user_id', userId);
18+
ga.getXMLAnswer(function(response) {
19+
if (response) {
20+
var locationData = JSON.parse(response);
21+
22+
// Populate catalog variables
23+
if (locationData.location) {
24+
g_form.setValue('location', locationData.location);
25+
}
26+
if (locationData.city) {
27+
g_form.setValue('city', locationData.city);
28+
}
29+
if (locationData.state) {
30+
g_form.setValue('state', locationData.state);
31+
}
32+
if (locationData.country) {
33+
g_form.setValue('country', locationData.country);
34+
}
35+
}
36+
});
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ========================================
2+
// SCRIPT INCLUDE (SERVER-SIDE)
3+
// ========================================
4+
// Name: LocationUtilsAjax
5+
// API Name: LocationUtilsAjax
6+
// Client callable: TRUE (checked)
7+
// Active: TRUE (checked)
8+
// ========================================
9+
10+
var LocationUtilsAjax = Class.create();
11+
LocationUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
12+
13+
getUserLocation: function() {
14+
var userId = this.getParameter('sysparm_user_id');
15+
var result = {};
16+
17+
if (userId) {
18+
// Get user record
19+
var userGR = new GlideRecord('sys_user');
20+
if (userGR.get(userId)) {
21+
// Get location reference
22+
if (!userGR.location.nil()) {
23+
// Get location details from cmn_location table
24+
var locGR = new GlideRecord('cmn_location');
25+
if (locGR.get(userGR.location)) {
26+
result.location = locGR.name.toString();
27+
result.city = locGR.city.toString();
28+
result.state = locGR.state.toString();
29+
result.country = locGR.country.toString();
30+
}
31+
}
32+
}
33+
}
34+
35+
return JSON.stringify(result);
36+
},
37+
38+
type: 'LocationUtilsAjax'
39+
});

0 commit comments

Comments
 (0)