How to fetch the filed values in the form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 12:30 PM
Hi,
Am new snow development.
I created the fields Fname, Lname, mail id, address and users( with choices 1,2,3). I want to see the related Fname, Lname, mail id, address values automatically when select the user values like 1,2,3. kindly suggest how to achieve it and what are the ways.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2025 12:33 AM - edited 06-20-2025 12:33 AM
Hello @pradeep01ti ,
To fetch field values from a form in ServiceNow, process them on the server, and then update other form fields, you use a combination of a Client Script and a Script Include. This ensures that complex logic and database queries are handled securely and efficiently on the server-side, while the client script manages the user interface interaction.
Here's a step-by-step breakdown with the code:
The Approach
- Client Script (on onChange This script runs on the client (the user's browser) when a specific field's value changes. It captures the new value and uses GlideAjax to asynchronously call a server-side Script Include.
- Script Include: This is a server-side script that performs the necessary database queries (e.g., fetching user details from sys_user) and processes the data. It then returns the processed data back to the client script.
- Client Script (Callback): After receiving the data from the Script Include, the client script's callback function updates the relevant fields on the form.
Step 1: Create the Script Include (Server-Side Logic)
The Script Include will be responsible for querying the user table and retrieving the first and last names.
- Name: getUserDetails (or a meaningful name of your choice)
- API Name: global.getUserDetails (automatically generated)
- Client callable: Checked (this is crucial for GlideAjax to work)
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
// Call a GlideAjax to fetch user details
var ga = new GlideAjax('getUserDetails');
ga.addParam('sysparm_name', 'getUserName');
ga.addParam('sysparm_user_id', newValue);
g_form.addInfoMessage(newValue + "Test");
ga.getXMLAnswer(function(response) {
g_form.addInfoMessage(response + "Testing");
var userData = JSON.parse(response);
g_form.setValue('fname', userData.first_name);
g_form.setValue('lname', userData.last_name);
});
}
Script Include:
var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserName: function() {
var userId = this.getParameter('sysparm_user_id');
var userGR = new GlideRecord('sys_user');
// if (userGR.get(userId)) {
// var result = {
// first_name: userGR.first_name.toString(),
// last_name: userGR.last_name.toString()
// };
userGR.addQuery('sys_id',userId);
userGR.query();
if (userGR.next()) {
var result = {
first_name: userGR.first_name.toString(),
last_name: userGR.last_name.toString()
};
gs.info("Test" + JSON.stringify(result));
return JSON.stringify(result);
}
// return JSON.stringify({ first_name: '', last_name: '' });
},
type: 'getUserDetails',
});
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ved