Look up record in catalog client script by sys_id

Jeff77
Tera Guru

I have a really simple use-case that I'm surprised I can't get working.  Here's the scenario.  Custom table, and I have a Lookup field in a Catalog Item request form where users select a record.  This returns the sys_id of the record, so far so good.  Now, what I'm trying to do is when a user updates that field, have an onChange Client script that will populate other read-only fields from the record they selected (reason being is that table is updated nightly with a full truncate/load operation so want to maintain the stae of the data when user submit their form).  I have tried a few options (including server Include Script call), most recent iteration is below.  Feeling stupid I can't figure this one out but up against a deadline, so hoping for some hints/guidance from someone feeling generous.  

Thanks in advance.  

var gr = new GlideRecord('custom_tablename');
var record_sysID = g_form.getValue('catalog_lookupfield');

if (gr.get(record_sysID)) {
	g_form.setValue('anotherCatalogField', gr.otherFieldnameFromTable);
}

 

1 ACCEPTED SOLUTION

Jeff77
Tera Guru

Thank you for the tips Michael.  I had been working on a server-side script call but was running into errors that I thought were because of syntax issues on my end.....the gist of the error was along the lines of : "'....missing name after . operator"

Turns out the cause was pretty simple.  The form in question was for instructors to select the class they want to make updates to......so I'd named one of the fields 'class'.....which, TIL is a reserved word in javascript, causing the issues.  

Anyway, got it working now....thanks! 

View solution in original post

3 REPLIES 3

Michael Jones -
Giga Sage

Client-side, GlideRecord is not the recommended approach. You will usually want to use GlideAjax to place a call server-side, query your record, and the pass the results back to the client-side for processing. 

A simple example would be: 

Script Include:

Name: exampleAJ

Client callable: True

Script: 

var exampleAJ = Class.create();
exampleAJ.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getRecord: function() {
	var rec_id = this.getParameter('sysparm_sys_id');
        var rec = new GlideRecord('custom_tablename');
        rec.get(rec_id);
        return rec.getValue('otherFieldnameFromTable');	
		
	},
	type: 'exampleAJ'
	
});


And client side: On Change of your reference field. 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var getEmail = new GlideAjax('exampleAJ');
	getEmail.addParam('sysparm_name', 'getRecord');
	getEmail.addParam('sysparm_sys_id', newValue);
	getEmail.getXML(setField);
	
	function setField(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		
		//set a field, or produce an alert for testing.
		g_form.setValue('anotherCatalogField', gr.otherFieldnameFromTable);
	}
	
}



Just a sample of course, but give it a try and see if it meets your needs. 

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Yes, you can refer the above code, GlideRecord is not recommended in the Client side scripting...

Jeff77
Tera Guru

Thank you for the tips Michael.  I had been working on a server-side script call but was running into errors that I thought were because of syntax issues on my end.....the gist of the error was along the lines of : "'....missing name after . operator"

Turns out the cause was pretty simple.  The form in question was for instructors to select the class they want to make updates to......so I'd named one of the fields 'class'.....which, TIL is a reserved word in javascript, causing the issues.  

Anyway, got it working now....thanks!