- 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 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-30-2024 01:50 AM
Yup, it worked I did some changes to the code, to better suit what I needed but it worked, also I was forgetting to check the "Client Callable" on the script include ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2024 02:19 AM
oh okay !
Thanks for the acceptance 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2024 11:10 AM
Yes, which is what I called out above.
It's unfortunate that none of my replies were marked as "Accept Solution" consider the information I provided or at a minimum...Helpful.
I'm glad the reply above helped you, but it copies a lot of what I already said even including a line from my own signature at the bottom...lol...
Take care!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!