What is use of getReference().

niveditakumari
Mega Sage

Hi, 

 

What is use of getReference(). 

Can anyone please give me one example for this. 

 

Regards, 

Nivedita

 

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Nivedita,

You can see an example here on the Docs site.

https://docs.servicenow.com/bundle/rome-application-development/page/script/client-scripts/concept/c...

You use it in a client script to retrieve the entire record of a reference field/variable, then you can use another column on the same record in the script to setValue of another field/variable, etc.

Here's an example which sets the value of a location variable to the location that is on the user record of the requested for.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

	var userref = g_form.getReference('requested_for', userLookup);
	  function userLookup(userref){
	    g_form.setValue('location', userref.location.toString());
	  }
}

View solution in original post

4 REPLIES 4

Cris P
Tera Guru
It can be used client side to grab the record in a reference field. g_form.getReference(‘caller’); If you use the above on incident table, you pass it the caller field. You will then be able to access the sys_user record for that caller You can also pass it a callback function which you can do some processing async (so it does not block user input)

Brad Bowman
Kilo Patron
Kilo Patron

Hi Nivedita,

You can see an example here on the Docs site.

https://docs.servicenow.com/bundle/rome-application-development/page/script/client-scripts/concept/c...

You use it in a client script to retrieve the entire record of a reference field/variable, then you can use another column on the same record in the script to setValue of another field/variable, etc.

Here's an example which sets the value of a location variable to the location that is on the user record of the requested for.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

	var userref = g_form.getReference('requested_for', userLookup);
	  function userLookup(userref){
	    g_form.setValue('location', userref.location.toString());
	  }
}

Kieran GFUK
Tera Contributor

It returns a gliderecord object of a given record. Note, don't use it, it's awful on performance.

Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!

Nirmalya Datta1
Kilo Expert
  • The functionality is about is the g_form getReference callback API
  • It is a ServiceNow Client Side Scripting
  • It’s extremely useful, but can also be a huge performance killer if not used correctly
  • Returns a Reference Object's record client-side

    1. Request a record from the database
    2. Requested record returned from the database
    3. Execute logic after the object returns from the server
  • It runs "asynchronously" when used with a callback function
  • If callback function is not used, it executes a "synchronous" call
  • Generic syntax is g_form.getReference()

Example: without a callback (Not Recommended because it is a synchronous call and might hamper system usability)

When: onChange
Field: caller_id

function onChange(control, oldValue, newValue, isLoading) {
 var caller = g_form.getReference('caller_id');
 if (caller.vip == 'true')
alert('Caller is a VIP!');
}

**Synchronous getReference() method calls lock the form until the requested record is returned from the database.

Example: with a callback (recommended, it is a asynchronous call and should not hamper system usability)

When: onChange
Field: caller_id

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

**Asynchronous execution allows users to continue using the form while the method call executes.

--> Please mark the answer helpful if it has helped you and mark the answer as correct