
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 07:39 AM
Hi, I'm learning servicenow and javascript and trying to create a client script which is setting 'company' filed to a caller's company. Unfortunately I can't get it right. "Answer" variable in client script is null.
this is my client script Set User Company (which is calling my Script Include - UserCompany)
Set User Company (client script)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var ga = new GlideAjax('UserCompany');
ga.addParam('sysparm_name', 'getUserCompany');
ga.addParam('sysparm_user_id', newValue);
ga.getXMLAnswer(analyze);
}
function analyze(answer) {
alert("answer: " + answer);
g_form.setValue('company', answer);
g_form.setReadOnly('company', true);
}
UserCompany (script include)
var UserCompany = Class.create();
UserCompany.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
initialize: function() {
},
getUserCompany: function() {
var userRecord = new GlideRecord('sys_user');
var user_id = this.getParameter('sysparm_user_id');
userRecord.get('sys_id', user_id);
var companyId = userRecord.getValue('company');
var companyRecord = new GlideRecord('core_company');
companyRecord.get('sys_id', companyId);
return companyRecord.name.getDisplayValue();
},
type: 'UserCompany'
});
But when I try to test my script include from "scripts- background" using this script, its working, I get proper company name, so the problem must be somewhere in the client script, but I can't find it!
var company = new UserCompany().getUserCompany();
gs.info(JSON.stringify(company));
I'm also modifying a little my script include for testing (to be able to get any user record):
var UserCompany = Class.create();
UserCompany.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
initialize: function() {
},
getUserCompany: function() {
var userRecord = new GlideRecord('sys_user');
// hardcoded user id
var id = '46d44a23a9fe19810012d100cca80666';
userRecord.get('sys_id', id);
var companyId = userRecord.getValue('company');
gs.info("companyId: " + companyId);
var companyRecord = new GlideRecord('core_company');
companyRecord.get('sys_id', companyId);
return companyRecord.name.getDisplayValue();
},
type: 'UserCompany'
});
I was also looking for some conflicting UI policies or client scripts, but I couldn't find any.
Can anyone help, please ? 🙂
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 07:46 AM
Hi,
update as this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var ga = new GlideAjax('UserCompany');
ga.addParam('sysparm_name', 'getUserCompany');
ga.addParam('sysparm_user_id', newValue);
ga.getXMLAnswer(function(answer){
alert("answer: " + answer);
g_form.setValue('company', answer);
g_form.setReadOnly('company', true);
});
}
Remove initialize() function and update as this
var UserCompany = Class.create();
UserCompany.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserCompany: function() {
var userRecord = new GlideRecord('sys_user');
var user_id = this.getParameter('sysparm_user_id');
userRecord.get(user_id);
var companyName = userRecord.company.getDisplayValue();
return companyName;
},
type: 'UserCompany'
});
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 08:29 AM
yes, the field is getting company name.
Can you tell me where should I look for possibly conflicting scripts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 08:30 AM
Hi,
Any UI policy on that table
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 02:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 08:06 AM
Hi there,
If you are getting a sys_id, you should be able to just pass that to it without specifying the sys_id column, i.e.:
companyRecord.get(companyId);
Also the line where you are doing this:
return companyRecord.name.getDisplayValue();
Can you try remove the getDisplayValue(); Since that field is just a string field anyway.
Other than that cannot see anythin else wrong with it. If you do a gs.info(companyRecord.name) (in your script include), what comes up in the logs?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 08:42 AM
Thanks for your answer, I'll keep that in mind. I think that what helped most was removing initialize() and moving all logic into one function on client script. But I must dive deeper into those ajax functions to know exactly what is going out there.