onchange script error: typeerror: cannot read properties of undefined (reading 'tostring') function

Jamesk
Tera Expert

Hi,

 

Apologies if this is a simple issue, I am just beginning my developer journey!

 

I am trying to create a client script that will update a custom customer approvers field (list type) with users of the assignment group "ACME Change Approvers" when the "Technical Approver" field is changed, the company = 'acme' and the type = 'normal' but I am getting the following error with my script:

 

onchange script error: typeerror: cannot read properties of undefined (reading 'tostring') function () { [native code] }

 

Below is the script I have created:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}


var company = g_form.getValue('company');
var type = g_form.getValue('type');
if (company === 'acme' && type === 'normal'); {

 

var acmeGroup = new GlideRecord('sys_user_group');
acmeGroup.addQuery('name', 'ACME Change Approvers');
acmeGroup.query();


while (acmeGroup.next()) {

var approver = acmeGroup.member.toString();
g_form.addOption('u_business_approver', approver, acmeGroup.member.getDisplayValue());
}
}
}

 

Can you help with troubleshooting this error?

 

Thanks

James

1 ACCEPTED SOLUTION

Joe S1
Kilo Sage

Hi James,

 

You can't perform a GlideRecord query from a client script. You're going to need to move the query to a Script Include and call the SI using GlideAjax.

 

GlideAjax Reference - https://developer.servicenow.com/dev.do#!/reference/api/tokyo/client/c_GlideAjaxAPI

View solution in original post

3 REPLIES 3

Joe S1
Kilo Sage

Hi James,

 

You can't perform a GlideRecord query from a client script. You're going to need to move the query to a Script Include and call the SI using GlideAjax.

 

GlideAjax Reference - https://developer.servicenow.com/dev.do#!/reference/api/tokyo/client/c_GlideAjaxAPI

DrewW
Mega Sage
Mega Sage

Your issue is this

var approver = acmeGroup.member.toString();

member is probably null.

 

You have to realize that the client side GlideRecord does not work the same way as the Server side one.

Docs are here

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/client/c_GlideRecordClientSideAPI?n...

 

So you need to write your code like this

var approver = "" + acmeGroup.member;

or

var approver = acmeGroup.member;

or

var approver = acmeGroup.getValue("member");

 

DrewW
Mega Sage
Mega Sage

I also just noticed this

acmeGroup.member.getDisplayValue()

 

Using getDispalyValue only works on Service Portal, Mobile, and Agent Workspace and it has to be like this

acmeGroup.getDisplayValue("member")