- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 04:47 AM - edited 08-29-2024 05:00 AM
Hi everyone, Im trying to make a client script, that will change the fields "email" and "country" when the "assigned_to" changes.
Im doing a Client Script "OnChange()", calling a script include, I will leave the code that Im using bellow.
Script Include:
var UserInfo = Class.create();
UserInfo.prototype = {
initialize: function() {},
getUserInfo: function(userId) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userId);
gr.query();
if (gr.next()) {
var results = {
"country": gr.location.country.getDisplayValue(),
"email": gr.getValue('email')
};
}
return JSON.stringify(results);
},
type: 'UserInfo'
};
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.addInfoMessage('Assigned to changed. New Value: ' + newValue);
var userSysId = newValue;
if (userSysId) {
var ga = new GlideAjax('UserInfo');
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('userId', userSysId);
ga.getXMLAnswer(response);
}
g_form.addInfoMessage(response + 'TesteResponse');
function response(answer) {
if (answer) {
g_form.addInfoMessage('Running the IF');
var returneddata = JSON.parse(answer);
g_form.setValue("u_assigned_to_country", returneddata.country);
g_form.setValue("u_assigned_to_email", returneddata.email);
}
}
}
When I change the assigned_to nothing happens, and it is not running the "if(answer)"
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 11:16 PM - edited 09-02-2024 01:44 AM
Hey @F_bio Santos
Kindly find below code. This is working for me.
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
g_form.addInfoMessage('Assigned to changed. New Value: ' + newValue);
var userSysId = newValue;
if (userSysId) {
var ga = new GlideAjax('UserInfo');
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('sysparam_assignedto', userSysId);
ga.getXML(callback);
}
// g_form.addInfoMessage(response + 'TesteResponse');
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var returneddata = JSON.parse(answer);
g_form.setValue("u_email", returneddata.email);
g_form.setValue("u_country", returneddata.country);
}
}
Script Include (**Make sure it is client callable)
var UserInfo = Class.create();
UserInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function() {
var userId= this.getParameter('sysparam_assignedto');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userId);
gr.query();
if (gr.next()) {
var jsonObj = {};
jsonObj.country= gr.location.country.getDisplayValue(),
jsonObj.email= gr.getValue('email');
}
return JSON.stringify(jsonObj);
},
type: 'UserInfo'
});
Output:
Kindly mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 05:13 AM - edited 08-29-2024 05:14 AM
Please refer to my reply above that mentions the GlideAjax cheat sheet for you to compare your code as well as my suggestion as it relates to the "client callable" script include. If you're curious about what code might be missing, open a brand new script include, don't save, just click and unclick the client callable checkbox, you'll see some additional code is added for client callable script includes. With the box checked, compare against your original script include and you'll see the gap.
The above may not be the only issue you have in your code...so please refer to the cheat sheet as mentioned.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2024 11:12 AM
I'm glad you found a solution that worked for you, but the goal here, at least for me with helping you, is for you to learn the actual issue, learn to troubleshoot, and learn how to fix it. Being provided the code to use is great and all, but that doesn't help you in the long term.
As I mentioned below, it's quite unfortunate that none of my replies have been marked with any sort of indication of acknowledgement that it was Helpful and/or helped guide you to a solution.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2024 07:42 PM - edited 08-30-2024 07:42 PM
Hi @Allen Andreas I actually did what you wrote in the comment above and it helped, I accepted the other solution because it worked even tho I didnt used the code that was provided like you said, it helped me knowing what I was doing wrong.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 05:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 06:49 AM
Hi @F_bio Santos ,
I have Made few Modifications to your code
In the Client Script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.addInfoMessage('Assigned to changed. New Value: ' + newValue);
var userSysId = newValue;
var ga = new GlideAjax('UserInfo');
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('sysparm_userId', userSysId);
ga.getXMLAnswer(function(answer) {
if (answer) {
g_form.addInfoMessage('Running the IF');
var returneddata = JSON.parse(answer);
g_form.setValue("short_description", returneddata.country);
g_form.setValue("description", returneddata.email);
} else {
g_form.addErrorMessage('No data returned for the selected user.');
}
});
}
In the Script Includes Part :
var UserInfo = Class.create();
UserInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function(userId) {
var ab = this.getParameter('sysparm_userId');
var gr = new GlideRecord('sys_user');
gr.get(ab);
gr.query();
if (gr.next()) {
var results = {
"country": gr.location.country.getDisplayValue(),
"email": gr.getValue('email')
};
}
return JSON.stringify(results);
},
type: 'UserInfo'
});
Output:
Values are populating in the short description and description Fields , please edit the fields which are need to be populated
Mark as Helpful , if this Answer works
thanks Regards
Badri