← Script library

Client Scripts

Populate Fields from Current User

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

JavaScript
function onLoad() {
  // Only run when creating a new record
  if (g_form.isNewRecord()) {

    // Configuration: Define which fields to populate
    var populateFields = true;

    // Get current user information
    var userId = g_user.userID;
    var userName = g_user.userName;
    var userFullName = g_user.firstName + ' ' + g_user.lastName;

    // Example 1: Populate caller with current user
    if (g_form.getValue('caller_id') === '') {
      g_form.setValue('caller_id', userId);
    }

    // Example 2: Populate opened_by with current user (usually automatic)
    // This is typically set automatically by ServiceNow
    // Uncomment only if you have a custom field
    /*
    if (g_form.getValue('opened_by') === '') {
      g_form.setValue('opened_by', userId);
    }
    */

    // Example 3: Get additional user details via GlideAjax
    // This fetches more detailed user information from the server
    var ga = new GlideAjax('UserInfoUtils');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_user_id', userId);

    ga.getXMLAnswer(function(response) {
      if (response) {
        try {
          var userDetails = JSON.parse(response);

          // Populate fields with user details
          if (userDetails.email && g_form.getValue('u_contact_email') === '') {
            g_form.setValue('u_contact_email', userDetails.email);
          }

          if (userDetails.phone && g_form.getValue('u_contact_phone') === '') {
            g_form.setValue('u_contact_phone', userDetails.phone);
          }

          if (userDetails.location && g_form.getValue('location') === '') {
            g_form.setValue('location', userDetails.location);
          }

          if (userDetails.department && g_form.getValue('u_department') === '') {
            g_form.setValue('u_department', userDetails.department);
          }

          // Optional: Show info message
          if (userDetails.vip) {
            g_form.addInfoMessage('You are marked as a VIP user. Your request will be prioritized.');
          }

          // Display open tickets count
          if (userDetails.openTickets > 0) {
            g_form.addInfoMessage('You currently have ' + userDetails.openTickets + ' open tickets.');
          }
        } catch (e) {
          console.error('Error parsing user details: ' + e);
        }
      }
    });

    // Example 4: Pre-populate custom message
    /*
    var welcomeMsg = 'Request created by ' + userFullName + ' (' + userName + ')';
    g_form.setValue('u_created_by_message', welcomeMsg);
    */
  }
}

How to use it

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

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.