- 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 04:48 AM - edited 08-29-2024 04:56 AM
Hi @F_bio Santos ,
What is the issue you are facing?
Also in your script include you need to receive the param set in your client script:
var user= this.getParameter('userid');
Your script include function should look like this :
function() {
var userid = this.getParameter('userid');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userId);
gr.query();
Also make sure the script include created is marked as client callable.
Please mark helpful/correct if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 05:02 AM
When I change the assigned_to nothing happens, and it is not running the "if(answer)"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 04:48 AM - edited 08-29-2024 04:50 AM
Unfortunately, you didn't provide any information as to what your issue is...is it not working? Is it throwing an error? What do you think is the problem? etc.
Instead, you've pasted your code here without any indication from you as to what you're stuck on.
Please refer to the this GlideAjax cheat sheet: https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... -- and consider reviewing your script include for the "client callable" checkbox to be check prior to having saved the record when you initially create it. If you don't check the box prior to saving, your script include will not be created properly with the correct script that enables this for being client callable.
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-29-2024 05:00 AM
When I change the assigned_to nothing happens, and it is not running the "if(answer)"