What is use of getReference in client side.

niveditakumari
Mega Sage

Hi, 

 

What is use of getReference in client side. I know we can get data from server side to client side but we can get data with GlideAjax as well then what is use of getReference. 

Can anyone please make me understand for it. 

 

Regards, 

Nivedita

 

 

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi @niveditakumari ,

The g_form.getReference() method requests the whole Record (one Object with all fields values) to the server. It may cause slowness to your Client Side action.

Many cases one or very few fields are necessary for us. So, instead of getReference we can use GlideAjax.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

5 REPLIES 5

Mike_R
Kilo Patron
Kilo Patron

See the docs

 

https://docs.servicenow.com/bundle/tokyo-application-development/page/app-store/dev_portal/API_refer...

 

GlideForm - getReference(String fieldName, Function callBack)

Returns the GlideRecord for a specified field.

If a callback function is present, this routine runs asynchronously. The browser (and script) processing continues normally until the server returns the reference value, at which time, the callback function is invoked. If a callback function is not present, this routine runs synchronously and processing halts (causing the browser to appear to hang) while waiting on a server response.

 

Important: It is strongly recommended that you use a callback function.

 

Callback function support for ServiceCatalogForm.getReference is available.

Note: Using this method requires a call to the server which requires additional time and may introduce latency to your page. Use this method with caution. For additional information, see Client script design and processing.
 
ParametersName Type Description
fieldNameStringName of the field.
callBackFunctionName of the call back function.
ReturnsType Description
GlideRecordGlideRecord object for the specified field.

If the specified reference cannot be found, it returns an initialized GlideRecord object where currentRow = -1 and rows.length = 0.

 

Example

function onChange(control, oldValue, newValue, isLoading) {
    g_form.getReference('caller_id', doAlert); // doAlert is our callback function
}
 
function doAlert(caller) { // reference is passed into callback as first arguments
   if (caller.getValue('vip') == 'true') {
      alert('Caller is a VIP!');
   }
}

 

 

Anurag Tripathi
Mega Patron
Mega Patron

HI,

With GlideAjax you have to write a script include as well and you decide what objects to return(you can chose to return just couple of fields from the referenced record) while with GetReference the whole object is returned so you can use any field from the referenced object. GlideAjax is more optimized.

-Anurag

Hi, 

 

Can you please provide one example and explain it for better understanding. 

 

Regards, 

Nivedita

 

 

Taking this example form Shawn's Blog here

Client Side (Client Script):
function onChange(control, oldValue, newValue, isLoading) {

      if (isLoading || newValue == '') {
              return;
      }
      var ga = new GlideAjax('asu_GetLocationData');
      ga.addParam('sysparm_name', 'getCampus');
      ga.addParam('sysparm_buildingid', g_form.getValue("u_building"));
      ga.getXML(updateCampus);
}
function updateCampus(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
      var clearvalue; // Stays Undefined
      if (answer) {
              var returneddata = answer.evalJSON(true);
              g_form.setValue("campus", returneddata.sys_id, returneddata.name);
      } else {
              g_form.setValue("campus", clearvalue);
      }
}
Server Side (Script Include):
var asu_GetLocationData = Class.create();
asu_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
      getCampus: function () {
              var buildingid = this.getParameter('sysparm_buildingid');
              var loc = new GlideRecord('cmn_location');
              if (loc.get(buildingid)) {
                      var campus = new GlideRecord('cmn_location');
                      if (campus.get(loc.parent)){
                              var json = new JSON();
                              var results = {
                                      "sys_id": campus.getValue("sys_id"),
                                      "name": campus.getValue("name")
                              };
                              return json.encode(results);
                      }
              } else {    
                      return null;
              }            
      }
});

 

In the example above, from script include only sys_id and name is returned from the referenced object so on the client side only those fields can be used.

 

If getReference is used then any field from the referenced object can be used as the entire object is returned.

 

-Anurag