|
| 1 | +# Auto-populate Location Variables in ServiceNow Catalog Items |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- Users must have their location field populated in their user profile (`sys_user.location`) |
| 10 | +- Location records (`cmn_location`) must have city, state, and country fields configured |
| 11 | + |
| 12 | +## Components |
| 13 | + |
| 14 | +### 1. Script Include |
| 15 | +- **Name:** LocationUtilsAjax |
| 16 | +- **Type:** Client Callable |
| 17 | +- **Purpose:** Retrieves user location details from server |
| 18 | + |
| 19 | +### 2. Catalog Client Script |
| 20 | +- **Type:** onLoad |
| 21 | +- **Purpose:** Fetches logged-in user's location data and populates catalog variables |
| 22 | + |
| 23 | +## Installation Steps |
| 24 | + |
| 25 | +### Step 1: Create Script Include |
| 26 | + |
| 27 | +1. Navigate to **System Definition > Script Includes** |
| 28 | +2. Click **New** |
| 29 | +3. Set **Name:** `LocationUtilsAjax` |
| 30 | +4. Check **Client callable** |
| 31 | +5. Paste the Script Include code |
| 32 | +6. Click **Submit** |
| 33 | + |
| 34 | +### Step 2: Create Catalog Variables |
| 35 | + |
| 36 | +Create these variables in your catalog item: |
| 37 | + |
| 38 | +- `location` - Single Line Text |
| 39 | +- `city` - Single Line Text |
| 40 | +- `country` - Single Line Text |
| 41 | +- `state` - Single Line Text |
| 42 | + |
| 43 | +### Step 3: Create Catalog Client Script |
| 44 | + |
| 45 | +1. Open your **Catalog Item** |
| 46 | +2. Go to **Related Links > Configure > Catalog Client Scripts** |
| 47 | +3. Click **New** |
| 48 | +4. Set **Type:** `onLoad` |
| 49 | +5. Paste the Catalog Client Script code |
| 50 | +6. Click **Submit** |
| 51 | + |
| 52 | +## Testing |
| 53 | + |
| 54 | +1. Ensure test user has location set in their profile |
| 55 | +2. Log in as test user and open the catalog item |
| 56 | +3. Verify variables are automatically populated |
| 57 | +4. Check browser console (F12) for any errors |
| 58 | + |
| 59 | +## Configuration Notes |
| 60 | + |
| 61 | +- Variable names in the script must match your catalog variable names exactly (case-sensitive) |
| 62 | +- Make variables **Read-only** if users shouldn't modify them |
| 63 | + |
| 64 | +## Security |
| 65 | + |
| 66 | +- Script validates user input |
| 67 | +- Only retrieves data for the current logged-in user |
| 68 | +- No sensitive data exposure beyond user profile |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +**Version:** 1.0 | **Date:** 2025-10-25 |
0 commit comments