diff --git a/Client-Side Components/Catalog Client Script/Populate Location Variables/README.md b/Client-Side Components/Catalog Client Script/Populate Location Variables/README.md new file mode 100644 index 0000000000..bf7fdf65a8 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Populate Location Variables/README.md @@ -0,0 +1,72 @@ +# Auto-populate Location Variables in ServiceNow Catalog Items + +## Overview + +This solution automatically populates location-related variables (Location, City, State, and Country) in Service Catalog items based on the logged-in user's profile information. + +## Prerequisites + +- Users must have their location field populated in their user profile (`sys_user.location`) +- Location records (`cmn_location`) must have city, state, and country fields configured + +## Components + +### 1. Script Include +- **Name:** LocationUtilsAjax +- **Type:** Client Callable +- **Purpose:** Retrieves user location details from server + +### 2. Catalog Client Script +- **Type:** onLoad +- **Purpose:** Fetches logged-in user's location data and populates catalog variables + +## Installation Steps + +### Step 1: Create Script Include + +1. Navigate to **System Definition > Script Includes** +2. Click **New** +3. Set **Name:** `LocationUtilsAjax` +4. Check **Client callable** +5. Paste the Script Include code +6. Click **Submit** + +### Step 2: Create Catalog Variables + +Create these variables in your catalog item: + +- `location` - Single Line Text +- `city` - Single Line Text +- `country` - Single Line Text +- `state` - Single Line Text + +### Step 3: Create Catalog Client Script + +1. Open your **Catalog Item** +2. Go to **Related Links > Configure > Catalog Client Scripts** +3. Click **New** +4. Set **Type:** `onLoad` +5. Paste the Catalog Client Script code +6. Click **Submit** + +## Testing + +1. Ensure test user has location set in their profile +2. Log in as test user and open the catalog item +3. Verify variables are automatically populated +4. Check browser console (F12) for any errors + +## Configuration Notes + +- Variable names in the script must match your catalog variable names exactly (case-sensitive) +- Make variables **Read-only** if users shouldn't modify them + +## Security + +- Script validates user input +- Only retrieves data for the current logged-in user +- No sensitive data exposure beyond user profile + +--- + +**Version:** 1.0 | **Date:** 2025-10-25 diff --git a/Client-Side Components/Catalog Client Script/Populate Location Variables/onLoad.js b/Client-Side Components/Catalog Client Script/Populate Location Variables/onLoad.js new file mode 100644 index 0000000000..7f5625072b --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Populate Location Variables/onLoad.js @@ -0,0 +1,37 @@ +// ======================================== +// CATALOG CLIENT SCRIPT +// ======================================== +// Name: Auto-populate Location Variables +// Type: onLoad +// UI Type: Desktop (or All) +// Applies to: [Your Catalog Item] +// ======================================== + +function onLoad() { + // Get current logged-in user's ID + var userId = g_user.userID; + + // Call Script Include to get user's location information + var ga = new GlideAjax('LocationUtilsAjax'); + ga.addParam('sysparm_name', 'getUserLocation'); + ga.addParam('sysparm_user_id', userId); + ga.getXMLAnswer(function(response) { + if (response) { + var locationData = JSON.parse(response); + + // Populate catalog variables + if (locationData.location) { + g_form.setValue('location', locationData.location); + } + if (locationData.city) { + g_form.setValue('city', locationData.city); + } + if (locationData.state) { + g_form.setValue('state', locationData.state); + } + if (locationData.country) { + g_form.setValue('country', locationData.country); + } + } + }); +} \ No newline at end of file diff --git a/Client-Side Components/Catalog Client Script/Populate Location Variables/scriptInclude.js b/Client-Side Components/Catalog Client Script/Populate Location Variables/scriptInclude.js new file mode 100644 index 0000000000..2a50f730e7 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Populate Location Variables/scriptInclude.js @@ -0,0 +1,39 @@ +// ======================================== +// SCRIPT INCLUDE (SERVER-SIDE) +// ======================================== +// Name: LocationUtilsAjax +// API Name: LocationUtilsAjax +// Client callable: TRUE (checked) +// Active: TRUE (checked) +// ======================================== + +var LocationUtilsAjax = Class.create(); +LocationUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { + + getUserLocation: function() { + var userId = this.getParameter('sysparm_user_id'); + var result = {}; + + if (userId) { + // Get user record + var userGR = new GlideRecord('sys_user'); + if (userGR.get(userId)) { + // Get location reference + if (!userGR.location.nil()) { + // Get location details from cmn_location table + var locGR = new GlideRecord('cmn_location'); + if (locGR.get(userGR.location)) { + result.location = locGR.name.toString(); + result.city = locGR.city.toString(); + result.state = locGR.state.toString(); + result.country = locGR.country.toString(); + } + } + } + } + + return JSON.stringify(result); + }, + + type: 'LocationUtilsAjax' +}); \ No newline at end of file