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

yes, the field is getting company name.
Can you tell me where should I look for possibly conflicting scripts?

Hi,

Any UI policy on that table

Regards
Ankur

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

Hi, 

I was looking for any UI policy that could interrupt this script

g_form.setReadOnly('company', true)

but I couldn't find any. there's 9 of them working on incident table and none of them is touching company field. What can I do more? Where can I find out what's interrupting?
find_real_file.png

Cris P
Tera Guru

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?

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.