How to Retrieve User details in ServiceNow Using Client Script and Script Include

In ServiceNow, we can dynamically fetch user information such as email and phone number based on a user selection in a form field. The solution provided below uses a Client Script in conjunction with a Script Include to retrieve the user’s email and phone number when a user is selected in the form. This integration ensures that the email and phone fields are auto-populated based on the selected user, improving efficiency and user experience.

Use Case

In this scenario, we want to auto-populate the email and phone fields in a form when the user is selected in a field. The fields will be filled automatically based on the user record in the sys_user table.

1. Client Script – onChange Script

The Client Script listens for changes in a specific field (for example, a User field), and when a new user is selected, it triggers the retrieval of the email and phone number for that user. Below is the code for the onChange Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   // Create a GlideAjax object to interact with the Script Include
   var ga = new GlideAjax('UserUtil');
   ga.addParam('sysparm_name','getUserEmailandPhone');
   ga.addParam('userID',newValue);  // Pass the selected user's sys_id

   ga.getXMLAnswer(callBack);  // Send the request and define the callback function

   // Callback function to process the response from the Script Include
   function callBack(answer){
      var response = JSON.parse(answer);  // Parse the JSON response
      g_form.setValue('u_email', response.email);  // Set the email field value
      g_form.setValue('u_phone', response.phone);  // Set the phone field value
   }
}

Explanation:

  • GlideAjax: The GlideAjax object is used to make an asynchronous call to a Script Include. This allows us to retrieve data from the server without refreshing the page.
  • addParam: The userID parameter is added to the request, which is the sys_id of the user selected in the form.
  • getXMLAnswer: This sends the request to the Script Include and executes the callback function once the response is returned.
  • g_form.setValue(): This method sets the value of the u_email and u_phone fields in the form based on the response from the Script Include.

2. Script Include – UserUtil

The Script Include named UserUtil is responsible for fetching the email and phone number of the user based on the sys_id passed from the client script. Below is the code for the Script Include:

var UserUtil = Class.create();
UserUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

   // Function to retrieve the user's email and phone number
   getUserEmailandPhone : function () {
      var userID = this.getParameter('userID');  // Retrieve the user ID passed from the client script

      gs.log('script include was triggered');  // Log to indicate the Script Include was triggered

      var userOBJ = {};  // Initialize an object to store the user details

      // Create a GlideRecord to query the sys_user table for the user
      var gr = new GlideRecord('sys_user');
      gr.addQuery('sys_id', userID);  // Query the user based on sys_id
      gr.query();

      if (gr.next()) {
         // If the user is found, populate the userOBJ with email and phone
         userOBJ.email = gr.getValue('email');
         userOBJ.phone = gr.getValue('phone');
      }

      return JSON.stringify(userOBJ);  // Return the user details as a JSON string
   },

   type: 'UserUtil'  // Define the Script Include type
});

Explanation:

  • GlideRecord: The GlideRecord is used to query the sys_user table for the user record using the sys_id passed from the client script.
  • getValue(): This method retrieves the values for the email and phone fields from the user record.
  • JSON.stringify(): The user details are returned as a JSON string, which can be parsed in the client script to populate the form fields.

How They Work Together

  1. When the user is selected in the form, the onChange client script triggers.
  2. The client script sends a request to the UserUtil Script Include using GlideAjax, passing the selected user’s sys_id.
  3. The Script Include queries the sys_user table and retrieves the user’s email and phone number.
  4. The Script Include returns the data as a JSON response, which the client script processes to populate the email and phone fields in the form.

Conclusion

This integration between a Client Script and a Script Include ensures that the email and phone number fields are auto-populated based on the selected user’s sys_id, improving the efficiency of data entry and ensuring consistency across records.


This content provides a detailed explanation of how both the Client Script and Script Include work together to achieve the goal of auto-populating email and phone fields based on the user selected in a form.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *