- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2025 09:19 AM
Hi,
Some help please not sure what am i missing here either of the user details are not populating at all.
These are the variables i created:
script include i created:
Catalog client script
Regards
CarolMa
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2025 05:17 PM
In this scenario, if user selects Yes, then Auto-populate the Requested for field with the currently logged in user's id and make the field read-only. You can achieve this using UI policy.
If user selects no, then leave it as is.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2025 08:16 PM
so with the script include approach where are you stuck?
what debugging did you do?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2025 12:25 PM - edited 06-16-2025 12:26 PM
Hello @CarolMa6
Script Include line#18: should not have comma at the end as it is the last one.
Script Include line#21: stringify is misspelled.
I believe you are trying to achieve something like below:
To achieve like above, here is all that you need:
1. UI Policy to hide variables:
2. onChange Client scripts = 2 ; onChange: request_for_you and onChange: name
onChange: request_for_you
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var requestor = g_form.getValue('request_for_you');
var userID = g_user.userID;
if (requestor === 'Yes') {
g_form.clearValue('user_name');
g_form.clearValue('title');
g_form.setMandatory('name', false);
g_form.setReadOnly('name', true);
g_form.setDisplay('user_name', true);
g_form.setDisplay('title', true);
// To populate other fields like pNumber, department and manager of the logged in user
var ga = new GlideAjax('SARBGetUserDetails');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_user', userID);
ga.getXMLAnswer(function(response) {
var userDetails = JSON.parse(response);
if (userDetails.error) {
g_form.showFieldMsg('name', userDetails.error, 'error');
return;
}
g_form.setValue('name', userID);
g_form.setValue('user_name', userDetails.user_name);
g_form.setValue('title', userDetails.title);
});
} else if (requestor === 'Select') {
g_form.setDisplay('user_name', false);
g_form.setDisplay('title', false);
g_form.clearValue('name');
g_form.clearValue('user_name');
g_form.clearValue('title');
g_form.setMandatory('name', true);
g_form.setReadOnly('name', false);
} else if (requestor === 'No') {
g_form.clearValue('name');
g_form.clearValue('user_name');
g_form.clearValue('title');
g_form.setMandatory('name', true);
g_form.setReadOnly('name', false);
g_form.setDisplay('user_name', false);
g_form.setDisplay('title', false);
}
}
onChange: name
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var usrVal = g_form.getValue('name'); // sys_id of selected user
var userID = g_user.userID;
// If name is cleared, clear related fields
if (!usrVal) {
g_form.clearValue('user_name');
g_form.clearValue('title');
return;
}
// If selected user is current user, skip AJAX call
if (usrVal === userID) {
return;
}
g_form.setMandatory('name', true);
g_form.setReadOnly('name', false);
var ga = new GlideAjax('SARBGetUserDetails');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_user', usrVal);
ga.getXMLAnswer(function(response) {
var userDetails = JSON.parse(response);
if (userDetails.error) {
g_form.showFieldMsg('name', userDetails.error, 'error');
return;
}
var userName = userDetails.user_name || '';
var userTitle = userDetails.title || '';
g_form.setValue('user_name', userName);
g_form.setValue('title', userTitle);
g_form.setDisplay('user_name', true);
g_form.setDisplay('title', true);
if (!userName) {
g_form.showFieldMsg('user_name', 'User Name is not available for the selected user.', 'info');
}
if (!userTitle) {
g_form.showFieldMsg('title', 'Title is not available for the selected user.', 'info');
}
});
}
Script Include: SARBGetUserDetails
var SARBGetUserDetails = Class.create();
SARBGetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var srn = this.getParameter('sysparm_user');
var ast = new GlideRecord('sys_user');
ast.addQuery('sys_id', srn);
ast.setLimit(1);
ast.query();
if (!ast.next()) {
return JSON.stringify({
error: 'P/C number does not exist, please provide the correct one.'
});
}
var userDetails = {
name: ast.getValue('name'),
user_name: ast.getValue('user_name'),
title: ast.title.getDisplayValue()
};
return JSON.stringify(userDetails);
},
type: 'SARBGetUserDetails'
});
NOTE#1: As you are using name variable type as Yes/No then by default Yes is selected, hence I set the Default value as Select.
NOTE#2: Please update the script include and client script based upon the userDetails you want to fetch and display.
Hope that helps!