- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2025 02:34 AM - edited 09-02-2025 02:35 AM
Hi,
I have a ref type variable. Based on that field the first and last name will be populated. below script is not working. Please help.
Client script :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getAffectedUserDetails');
ga.addParam('sysparm_name', 'getUserFirstName');
ga.addParam('sysparm_user_id', newValue);
ga.getXML(SetFirstName_ref);
function SetFirstName_ref(response) {
var first_name = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('first_name', first_name);
}
ga = new GlideAjax('getAffectedUserDetails');
ga.addParam('sysparm_name', 'getUserLastName');
ga.addParam('sysparm_user_id', newValue);
ga.getXML(SetLastName_ref);
function SetLastName_ref(response) {
var last_name = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('last_name', last_name);
}
}Script include:
var getAffectedUserDetails = Class.create();
getAffectedUserDetails.prototype = {
initialize: function() {},
getUserFirstName: function() {
gs.addInfoMessage("test");
var user_sys_id = this.getParameter('sysparm_user_id');
var user = new GlideRecord('sys_user');
user.addQuery("sys_id", user_sys_id);
user.query();
if (user.next()) {
//gs.addInfoMessage(user_sys_id);
return user.getValue("first_name");
}
},
getUserLastName: function() {
var user_sys_id = this.getParameter('sysparm_user_id');
var user = new GlideRecord('sys_user');
user.addQuery("sys_id", user_sys_id);
user.query();
if (user.next())
return user.getValue("last_name");
},
type: 'getAffectedUserDetails'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2025 03:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2025 03:02 AM
Is your variable the reference to the user table? Because that would make the most sense, otherwise it will never work, if you can just add any email address in there, it is prone to mistakes and you won't ever get the name from the user record.
Assuming my assumption is correct, you can just add the name fields as dependent on the email address.
However, why would you ever need these two values separately in two different variables? Why not just handle that after the item is submitted, since the name is always the same and coming from the user table?
What is your business case here?
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2025 03:20 AM
no scripting required if
1) your 1st variable is reference to sys_user
2) other 2 variables are of string type
You can use auto populate feature which came from Utah onwards
Auto-populate a variable based on a reference type variable (Utah)
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
09-02-2025 03:41 AM
Thank you for marking my response as helpful.
I believe I also answered your question and it has no scripting in it.
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct as well 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
09-02-2025 03:40 AM
