Skip to Content

Populate Fields from Current User

Automatically populate form fields with the current logged-in user's information (e.g., pre-fill requester details).

onLoad Table: incident
#onLoad #current-user #auto-populate #user-info #glideajax

Script Code

JavaScript
1function onLoad() {
2  // Only run when creating a new record
3  if (g_form.isNewRecord()) {
4
5    // Configuration: Define which fields to populate
6    var populateFields = true;
7
8    // Get current user information
9    var userId = g_user.userID;
10    var userName = g_user.userName;
11    var userFullName = g_user.firstName + ' ' + g_user.lastName;
12
13    // Example 1: Populate caller with current user
14    if (g_form.getValue('caller_id') === '') {
15      g_form.setValue('caller_id', userId);
16    }
17
18    // Example 2: Populate opened_by with current user (usually automatic)
19    // This is typically set automatically by ServiceNow
20    // Uncomment only if you have a custom field
21    /*
22    if (g_form.getValue('opened_by') === '') {
23      g_form.setValue('opened_by', userId);
24    }
25    */
26
27    // Example 3: Get additional user details via GlideAjax
28    // This fetches more detailed user information from the server
29    var ga = new GlideAjax('UserInfoUtils');
30    ga.addParam('sysparm_name', 'getUserDetails');
31    ga.addParam('sysparm_user_id', userId);
32
33    ga.getXMLAnswer(function(response) {
34      if (response) {
35        try {
36          var userDetails = JSON.parse(response);
37
38          // Populate fields with user details
39          if (userDetails.email && g_form.getValue('u_contact_email') === '') {
40            g_form.setValue('u_contact_email', userDetails.email);
41          }
42
43          if (userDetails.phone && g_form.getValue('u_contact_phone') === '') {
44            g_form.setValue('u_contact_phone', userDetails.phone);
45          }
46
47          if (userDetails.location && g_form.getValue('location') === '') {
48            g_form.setValue('location', userDetails.location);
49          }
50
51          if (userDetails.department && g_form.getValue('u_department') === '') {
52            g_form.setValue('u_department', userDetails.department);
53          }
54
55          // Optional: Show info message
56          if (userDetails.vip) {
57            g_form.addInfoMessage('You are marked as a VIP user. Your request will be prioritized.');
58          }
59
60          // Display open tickets count
61          if (userDetails.openTickets > 0) {
62            g_form.addInfoMessage('You currently have ' + userDetails.openTickets + ' open tickets.');
63          }
64        } catch (e) {
65          console.error('Error parsing user details: ' + e);
66        }
67      }
68    });
69
70    // Example 4: Pre-populate custom message
71    /*
72    var welcomeMsg = 'Request created by ' + userFullName + ' (' + userName + ')';
73    g_form.setValue('u_created_by_message', welcomeMsg);
74    */
75  }
76}

How to Use

1. Create an onLoad Client Script on your table 2. Customize which fields to populate from current user 3. Optionally create the UserInfoUtils Script Include for detailed info (see Script Includes section) 4. Update field names to match your table structure 5. Test by creating a new record while logged in as different users

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts