
- 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 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:02 AM
Thank you, almost working 🙂 Don't know why this doesn't work:
g_form.setReadOnly('company', true);
Field is still editable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2022 08:15 AM
Hi,
Glad to know.
use this
g_form.setReadonly('company', true)
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:18 AM
Hi,
are you sure no other script is making the field editable?
is the field getting populated with company name?
this looks good to me.
g_form.setReadOnly('company', true)
regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader