Error Message Assistance: onChange script error: TypeError: GlideRecord is not a constructor function

Emmon Johnson
Giga Contributor
Hey all,

I am currently working on a ServiceNow Application for our COVID testing site that will allow them to create a database for the testing registrations. (We're not storing results, the purpose of this is to make reporting easier and to have a registration database that can be used to easily identify a test to a person in case Upstate only returns the barcode with the results). I work for a Higher Ed insinuate and the Spring Semester starts next week so we're looking to have this ready for deployment before our students come back.
 
I have a Self-Service form working perfectly, but our user wants to add the ability to print a label for the test tubes from this as well. I have that working on the 'itil' side (UI Action that points to a printable UI page), but I want to streamline the flow of this. To do this I am working on creating a 'Create New' form in the 'itil' side (similar to the create new for the incident table).

We currently have the system setup for a contact-less registration. We have a Proxy Card reader that they can use so that person getting tested can scan their Proxy Cards and the system will pull their information and will automatically populate the form for the worker. I have a client script that works on the self-service side that when the field changes will query the sys_user table for the ID number and will return the name, but when I use that in the 'itil' form I get the following error:
 
find_real_file.png
(Error message when I use the GlideRecord query)
 
In troubleshooting I went to the incident form and copied a couple of the client scripts there do similar queries and I run into the same issue.
 
In researching this issue, I understand that ServiceNow is making updates and changes to the way that we can query data from the system, and from my understanding they are moving these types of queries to be Server side using GlideAjax and removing support for the client-side DOM (JavaScript) access.
 
find_real_file.png
(Above screenshot is from the client script on the incident table that populates the building/room fields on our incident form when the caller field is changed)
 
My main issue that that my Glide Ajax skill set is not really strong (I never really have used it before). I have a couple of questions on this:

1. I was hoping to get some advice on how to best approach this. I'm working on brushing up my AJAX and have gone thru some of the previous responses that are similar to this, but this is new territory for me.

2. My organization has a few client scripts in our Incident and Request Management forms that use the GlideRecord to query various tables in order to populate fields on our forms. Will we need to go thru those scripts and convert them to use the Glide Ajax method?

I hope this makes sense and I appreciate any assistance or pointers that anyone can offer.
6 REPLIES 6

Not applicable

2 - no

1 - set your GlideAjax SI to client callable. Do not use GlideRecord in client scripts as it will fail. Can you post the AJAX + Client Script where you cann it, so we can check it out together?

 

 P.S. GlideRecord in UI Action - thats fine but in Client Script - are you sure?

Hi Joro, thanks for the response!

Below is what I have so far. The issue I am having right now is that I can't seem to get into the getUserInfo function in the Script Include to call properly. It doesn't appear to be triggering and I keep getting a null returned. (In the script include I also tried testing by having it just return a test value with nothing else).

Client Script:

//Code below tries to use AJAX
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading)
	{
		return;
	}

	//Create new AJAX Call
    var ga = new GlideAjax('covid_lookup_user_proxid');
	var prox = g_form.getValue('u_proxid');
	
	//ga.addParam('', ''); Shell code for adding parameters to the AJAX call
	//ga.addParam('', g_form.getValue('')); Shell code for adding a field variable to the
	alert('Setting sys_proxid to: '+ prox);
	ga.addParam('sysparm_name', 'getUserInfo'); //Function name
	ga.addParam('sys_proxid', prox);
	alert('before XML:');
	ga.getXML(getUserInfoAJAX); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
	alert('after XML');
}

// Callback function to process the response returned from the server
function getUserInfoAJAX(response)
{
	alert('response:' + response);
	var answer = response.responseXML.documentElement.getAttribute("answer");
	alert(answer);
	g_form.setValue('name', answer);
}

Script Include:

 

var covid_lookup_user_proxid = Class.create();

covid_lookup_user_proxid.prototype = Object.extendsObject(global.AbstractAjaxProcessor, 
{
	getUserInfo:function()
	{
		var retVal = ''; //Return value
		alert('inside script include');
		//var  = this.getParameter(''); //shell for retrieving the parameter from the client script
		var proxid_number = this.getParameter('sys_proxid'); //Retrieve ProxID number
		
		var user = new GlideRecord('sys_user');
		user.addQuery('u_proxid', proxid_number);
		var found_user = "No";
		user.query();
		/*if(user.next())
		{
			//g_form.setValue('name',user.sys_id);
			found_user = "Yes";
			retVal = user.sys_id;
			//g_form.setValue('first_name', count);
			//g_form.setValue('a_number', '');
		}*/
		retVal = 'test';
		return retVal;
	},
	type:'covid_lookup_user_proxid',
	isPublic: true
	
});

 

Here are the settings on the Script Include as well:
find_real_file.png

update below line. 

 

    var ga = new GlideAjax('covid_lookup_user_proxid');

 

You should use scope name

 

eg:

 

    var ga = new GlideAjax('<your scope name>.covid_lookup_user_proxid');

Also,, please remove alert() from script include, to check the log result use gs.info()

alert works at client side.