Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi Jim , 

your suggestion works for onload but 

I need to write a onchagne cilent script as well , OPID field is editable so user can manually change to different name if they wants to . 

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);
  }
}