Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client script gets null answer from script include

Beata I_owiecka
Kilo Guru

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 ? 🙂

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you, almost working 🙂 Don't know why this doesn't work:

g_form.setReadOnly('company', true);

 Field is still editable.

Hi,

Glad to know.

use this

g_form.setReadonly('company', true)

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader