g_form.getReference returns an undefined value

Jon23
Mega Sage

I am currently trying to create a child ticket with all the information from the parent in it.

Using the new button on the related list 'Facilities Request->Parent';

Capture.PNG

the following client script is triggered:

function onLoad() {

    if(g_form.isNewRecord()){

          var dad = g_form.getValue('parent');

          if(dad != ''){

                var parentRecord = g_form.getReference('parent');

                g_form.setValue('opened for', parentRecord.opened_for);

                g_form.setValue('location', parentRecord.location);

      jslog("JW-DEBUG: " + parentRecord.opened_for);

      jslog("JW-DEBUG: " + parentRecord.location);

          }

    }

}

function onLoad() {

    if(g_form.isNewRecord()){

          var dad = g_form.getValue('parent');

          if(dad != ''){

                var parentRecord = g_form.getReference('parent');

                g_form.setValue('opened for', parentRecord.opened_for)

The location field is correctly populated on my child ticket, however, the opened_for is not.

Looking at the javascript log i see the following:

JW-DEBUG: undefined

JW-DEBUG: b466f2800a0a3c7e00b84e0d16c206ae

Both fields are reference fields so I am confused why one works and one does not - any suggestions?

20 REPLIES 20

Not confused, makes perfect sense. I did some playing around and got it to work. It was those extra fields fields where it didn't work.



Thanks for taking the time to explain this. I have a greater understanding now.


Tod Toter


So how do you deal with those situations then? You have explained what the issue is but have not provided a solution.

HV1
Mega Guru

Please try below line:



  1. g_form.setValue('opened_for', parentRecord.opened_for, parentRecord.opened_for.getDisplayValue());  


Regards,


Hardik Vora


Derek Culberts1
Kilo Contributor

Hi jwalton, I was having issues with g_form.getReference while testing Helsinki. I was able to resolve by migrating to a client callable script include model. This is preferred over the getReference option anyway because it utilizes Server power and not client. g_form.getReference actually creates a GlideRecord on the client side, and can cause slowness. Try something similar to below.



Carate a new Script Include, (make sure you click the "Client Callable" button).


var sc_getUserData = Class.create();


sc_getUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getData: function() {


  //gs.log('Debug>>> is in getData');


  var usrData = '';


  var usrId = this.getParameter('sysparm_sys_id');


  //gs.log('Debug>>> ' + usrId);


  var gr = new GlideRecord('sys_user');


  gr.addQuery('sys_id', usrId);


  gr.query();


  while (gr.next()) {


  usrData = gr.user_name+','+gr.email+','+gr.phone+','+gr.title+','+gr.department.getDisplayValue();


  //gs.log('Debug>>> usrData = ' + usrData);


  }


  return usrData;


  },



  type: 'sc_getUserData'


});




Create an onChange client script to go along with that.



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }


  //Type appropriate comment here, and begin script below


  var ga = new GlideAjax('sc_getUserData');


  ga.addParam('sysparm_name', 'getData');


  ga.addParam('sysparm_sys_id', newValue);


  ga.getXML(callBack);


}




function callBack(response) {


  var usrArr = response.responseXML.documentElement.getAttribute("answer").split(',');


  //g_form.setValue('network_logon',usrArr[0].toString());


  g_form.setValue('u_email', usrArr[1].toString());


  g_form.setValue('u_phone', usrArr[2].toString());


  g_form.setValue('u_title', usrArr[3].toString());


  g_form.setValue('u_department', usrArr[4].toString());


  alert(usrArr);


}




This seemed to work much better for me. Its important that the Script Include be Client Callable, that your client script includes the line ga.addParam('sysparm_name', 'getData'). The rest you should be able to tweak to the names of the fields you are trying to populate. Hope this helps!


I Agree, to improve performance at client side we must avoid using Glide record and get_reference() method.


GlideAjax is recommended to use at client side to fetch data from server.


Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande