My client script is not executing. Any error message or info message also not showing up

srinivasrao
Tera Contributor
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.addInfoMessage("I am in ");
g_form.addErrorMessage (" i am here ");
function concatenateCallerIdAndStorename(callerID) {
// Perform an asynchronous GlideAjax call to fetch user details
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name', 'getDetails'); // Correct function name in script include
ga.addParam('sys_id', callerID);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
var userDetails = JSON.parse(answer);
var concatenatedValue = userDetails.user_id + " - " + userDetails.u_store_name;
 
// Set the concatenated value in a custom field (e.g., u_caller_details)
g_form.setValue('u_caller_details', concatenatedValue);
}
});
}
 
// Call the function to concatenate caller ID and department
concatenateCallerIdAndStorename(newValue);
}
 
-------------------------------
Script Include:
var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDetails: function() {
var sys_id = this.getParameter('sys_id');
var user = new GlideRecord('sys_user');
user.get(sys_id);

var details = {
user_id: user.user_name.toString(),
u_store_name : user.u_store_name.getDisplayValue().toString()
};

return JSON.stringify(details);
}


});
7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

So you are saying that also these at the start of your script don't trigger?

g_form.addInfoMessage("I am in ");
g_form.addErrorMessage (" i am here ");
 
Or move these statements above the isLoading check. Is it then still not triggered?
 
Is the onChange field set correct?
Is the UI type set correct? 

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Sandeep Rajput
Tera Patron
Tera Patron

@srinivasrao Please update line ga.addParam('sys_id', callerID); with the following

ga.addParam('sysparm_sys_id',callerID);

in client script 

 

In script include, replace var sys_id = this.getParameter('sys_id'); with the following.

var sys_id = this.getParameter('sysparm_sys_id');

 

Also, check the following in the client script.

1. UI Type: Make sure that UI Type is correctly set

2. Global: Make sure that either Global is checked or correct view is selected

3. Active: Check that Active checkbox is checked.

 

Hope this helps.

 

 

Community Alums
Not applicable

Hi Srinivasarao,

 

Can you please check in script include form check the client callable checkbox as true and in client script on change needs to select the correct field then you can use below script

 

Script Include:
var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDetails: function() {
var userobj={};
var user = new GlideRecord('sys_user');
if(user.get(this.getParameter(sysparm_user_id)){
userobj.callerId: user.getValue('user_name').toString();
userobj.department : user.getValue('department').toString();
}
};

return JSON.stringify(userobj);
}

});
 
-------------------------------------------------------------------
Client Script
 var userselected=g_form.getValue('here you need to provide the field value'); // u_caller_details
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name''getDetails'); // Correct function name in script include
ga.addParam('sysparm_user', userselected);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
var userDetails = JSON.parse(answer);
var concatenatedValue = userDetails.user_id + " - " + userDetails.u_store_name;
 
// Set the concatenated value in a custom field (e.g., u_caller_details)
g_form.setValue('u_caller_details', concatenatedValue);
}
});