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

update this code


  1. var reqByName = g_form.getReference('opened_for');  
  2.  
  3.                 if (reqByName != ''){  
  4.                       g_form.setValue('opened_for_id',reqByName.user_name);  
  5.        
  6.                 }  

Updated code:



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




                var reqByName = g_form.getReference('opened_for');



                if (reqByName != ''){


                      g_form.setValue('opened_for_id', reqByName.user_name);


   


                }




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


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


      jslog("JW-DEBUG: " + reqByName);




          }


    }


}



JS Log:



JW-DEBUG: undefined


JW-DEBUG: c5b48cbb0a0a3c7e01a3328fd5bb601a


JW-DEBUG: [object Object]


Brian O_Donnell
Tera Guru

I was having the same problem - it is because the getReference is returning an object of a higher level table that does not have that field defined.   You'll need to use a GlideRecord so that you get back the correct object.



something like:



var grParent = new GlideRecord('parent table name');


if (grParent.get(g_form.getValue('parent'))) {


      //your logic goes here...


      g_form.setValue('opened_for', grParent.opened_for);  


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


}


Your work around works perfectly! Thanks! But why doesn't getReferenc(fieldname) returns undefined? I am following the documentation GlideForm (g form) - ServiceNow Wiki   and i am using this function as expected. Is this a bug? I would like to use this function. I shouldn't have to work around this function...



Thanks for your help,


Tod Toter


The issue, at least in this case, has to do with extended tables and the way the fields are accessible as opposed to a problem with getReference.



What is happening here is that I was trying to get a value from a table, let's use Task as the example since it is used heavily.   The reference field that I was calling for was a child of Task (let's use Incident), but the relationship is based on the parent Task table.   The relationship allows any child of the Task table to be associated, since they are also Tasks - such as Change and others.   I was trying to obtain a value that only exists on the Incident table, not on the parent Task table like caller_id.   So the getReference is looking at the root table, or parent, and cannot find that field since it is only defined on the child.   Another example is:



Table 1 fields


A


B


C



Table 2 (extended from Table 1) fields


A [table 1]


B [table 1]


C [table 1]


D



If you have a list of items from table 1, you will see the items in table 2 since it is extended.   If you try to get a value out of field D from Table 1, it doesn't exist even though items in Table 2 are of the same base type as Table 1.   So you have to instantiate a specific call to Table 2 in order to get a value out of field D.   So getReference will work to get A, B, or C, but not D.



Hope that made sense and wasn't more confusing instead as I don't remember exactly why it was happening, but I think this is the concept that caused the issue.