- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 12:09 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2023 07:15 AM
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.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 12:13 PM
See the docs
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.
fieldName | String | Name of the field. |
callBack | Function | Name of the call back function. |
GlideRecord | GlideRecord 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!'); } }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 12:15 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 11:54 PM
Hi,
Can you please provide one example and explain it for better understanding.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2023 06:59 AM
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.