Catalog Client Script Dot Walk

ceraulo
Mega Guru

Hello!

I have a catalog client script shown below. It aims to get the region of the location variable from the cmn_location table.
My script returns undefined when I used getDisplayValue. If I remove getDisplayValue, it returns the sys_id.

function onLoad() {

    var loc = g_form.getValue('var_loc');
	
    var gr = new GlideRecord('cmn_location');
    gr.addQuery('sys_id', var_loc);
    gr.query(myCallbackFunction);

    function myCallbackFunction(gr) {
        while (gr.next()) { 
			
			alert(gr.u_region.getDisplayValue());

        }
    }

}

Please help!

Thank you.

12 REPLIES 12

It is a reference field.

What is the type of "u_region" field?

It is a reference field.

Then use this:

var location = g_form.getReference(‘var_loc’, showRegion);

function showRegion(location) {
  alert(location.u_region.<display field name>);
}

Brad Bowman
Kilo Patron
Kilo Patron

Since you're doing this onLoad, it would be a lot easier to do in the workflow or a business rule to use server scripting where you can dot-walk directly.  If it has to be shown on the client when the initial request form loads or whatever, then the best way to do this is with a GlideAjax call to a script include since it's a reference field on a referenced table field.  You can also add the same to an onChange catalog client script for when the location changes if that is needed. 

Your client script will look similar to this

function onLoad() {
    var ga = new GlideAjax('LocationLookup'); //Name of the Script Include 
    ga.addParam('sysparm_name', 'getRegion'); //name of function in script include 
    ga.addParam('sysparm_loc', g_form.getValue('var_loc'));
    ga.getXML(locCallback);
}

function locCallback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
}

Next, create a Script Include with the same name used in the GlideAjax call, ensuring that the Client callable box is checked.  Your script will look like this

var LocationLookup = Class.create();
LocationLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, { 
 
  getRegion: function() { 
   var loc = this.getParameter('sysparm_loc'); 
   var locGR = new GlideRecord('cmn_location');
   locGR.addQuery('sys_id', loc);
   locGR.query();
   if(locGR.next()) {
    return locGR.u_region.getDisplayValue();
   }
  },
        
type: 'LocationLookup'
});