Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

getReference is not working

vahini
Giga Guru

Hi all , 

I have a requirement to create a reference field called 'OPID'  referenced  to sys_user table and a tex field  called

'Name ' . 

OPID field is auto populated when form loads ( did this by adding javascript:gs.getUserID(); to the default value of OPID field  )  it works fine . 

and wrote a onload client script to populate the name field based on whats in OPID field .

 function onLoad() {
//Type appropriate comment here, and begin script below

var reqRec = g_form.getReference('u_opid');
var grUser = new GlideRecord('sys_user');

grUser.get(reqRec.sys_id);

g_form.setValue('u_name', grUser.name);

}

 

I wrote the same script for different catalog items  and they all working fine but now the same script is throwing below error . How do i fix this ? Please suggest . 

find_real_file.png

1 ACCEPTED SOLUTION

Correct.  I would change your current onLoad script to an onChange script and use the default for the initial data.  The onChange could look like this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  //skip when loading, the default value will do the trick instead
  if (isLoading) {
    return;
  }

  //clear the name when the reference field is cleared
  if (newValue === "") {
    g_form.setValue("u_name", "");
    return;
  }

  var user = g_form.getReference("u_opid", showCallerName);

  function showCallerName(user) {
    g_form.setValue("u_name", user.name);
  }
}

 

View solution in original post

6 REPLIES 6

Harsh Vardhan
Giga Patron

try now

 

function onLoad() {
//Type appropriate comment here, and begin script below

var reqRec = g_form.getReference('u_opid', showcallerTitle);

function showcallerTitle(reqRec) {

g_form.setValue('u_name', reqRec.name);

}

 

}

SanjivMeher
Mega Patron
Mega Patron

You script should be something like

 

function onLoad() {
//Type appropriate comment here, and begin script below

 g_form.getReference('u_opid', doAlert); // doAlert is our callback function
}
 
function doAlert(caller) { // reference is passed into callback as first arguments
   g_form.setValue('u_name', caller.name);
}

Please mark this response as correct or helpful if it assisted you with your question.

Phuong Nguyen
Kilo Guru

One thing to note is that you should not use a GlideRecord query in client script as it is not best practice. That being said, you can use the examples provided by others or shorten it a little with the use of anonymous function:

 

function onLoad() {
//Type appropriate comment here, and begin script below

var reqRec = g_form.getReference('u_opid', function(reqRec){
  g_form.setValue('u_name', reqRec.name);
});

 

Regards,

Phuong

If you find my suggestions helpful, please mark it as correct or helpful to help out others as well 🙂

Jim Coyne
Kilo Patron

Instead of a client script, you could set the default value for that field as well:

javascript:gs.getUserDisplayName();