- 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 02:51 AM
Hi @Rosy14 ,
update the script include as below
client callable script include should always extend from "AbstractAjaxProcessor"
var getAffectedUserDetails = Class.create();
getAffectedUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var res = {};
gs.info("getAffectedUserDetails test");
var user = new GlideRecord('sys_user');
if (user.get(this.getParameter('sysparm_user_id'))) {
res.first_name = user.getValue('first_name')
res.last_name = user.getValue('last_name')
}
return JSON.stringify(res)
},
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'
});
you don't have to call the server 2 times you can get the response in one call (this is not the issue though)
you can update the client script as (this is optional the issue is with the script include but as a better approach you can update the client script as below)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getAffectedUserDetails');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_user_id', newValue);
ga.getXMLAnswer(SetFirstName_ref);
function SetFirstName_ref(answer) {
if (answer) {
var userDetails = JSON.parse(answer)
g_form.setValue('first_name', userDetails.first_name);
g_form.setValue('last_name', userDetails.last_name);
} else {
alert('no response')
}
}
}
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2025 03:06 AM
It is still not calling
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2025 03:13 AM
Add logging and provide more information about what you do and don't get. Repeating 'it's not calling' is not going to help us in helping you.
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
The script include is not calling from client script. tried to show one info message from this.
As mentioned added this on script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2025 03:16 AM
Hi @Rosy14 ,
did you update both client script and script and check?
Alos check if there any ACL on the script include and the user you are testing with have the role
go to ACLs and check name contains the name of the script include and operation = execute and type is client_callable_script_include
OR
you can try the no-code auto populate functionality here (refer below blogs)
Using the Variable Auto-populate Feature with Vari... - ServiceNow Community
Auto-populate a variable based on a reference type... - ServiceNow Community
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
